Page 1 of 1

selectable element after last character in ListviewHeader

Posted: 18 Jun 2019, 22:47
by wyvern010
As the topic says:
Image
<UserControl x:Class="WpfApp1.SSC.PersonsListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp1.SSC"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <ListView Grid.Row="2" ItemsSource="{Binding cbPersons}">
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.Header>
                        <GridViewColumnHeader Tag="Firstname" MouseLeftButtonDown="GridViewColumnHeader_MouseLeftButtonDown">First name</GridViewColumnHeader>
                    </GridViewColumn.Header>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate >
                            <TextBlock Text="{Binding Firstname}" Style="{StaticResource TextColor}" ></TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn>
                    <GridViewColumn.Header>
                        <GridViewColumnHeader Tag="Lastname" MouseLeftButtonDown="GridViewColumnHeader_MouseLeftButtonDown">Last name</GridViewColumnHeader>
                    </GridViewColumn.Header>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Lastname}" Style="{StaticResource TextColor}" ></TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</UserControl>

i dont know what is, but i think its ugly.

Re: selectable element after last character in ListviewHeader

Posted: 19 Jun 2019, 00:22
by digimbyte
Show code please, a tiny picture doesn't explain anything

Re: selectable element after last character in ListviewHeader

Posted: 19 Jun 2019, 11:44
by wyvern010
Show code please, a tiny picture doesn't explain anything
Updated.

Re: selectable element after last character in ListviewHeader

Posted: 20 Jun 2019, 13:10
by sfernandez
It is a bug in the GridViewHeaderRowPresenter, because it is wrapping the GridViewColumnHeader you specified with another one. I created a report in our bugtracker for this: https://www.noesisengine.com/bugs/view.php?id=1502

While we fix it you can workaround this issue by specifying directly the header text in the GridViewColumn:
<GridViewColumn Header="First name"/>
And you can handle the header clicks in the ListView control, checking if the sender of the event is a GridViewColumnHeader:
<ListView ... ButtonBase.Click="GridViewColumnHeader_Click">
private void GridViewColumnHeader_Click(object sender, MouseButtonEventArgs e)
{
  if (sender is GridViewColumnHeader) { ... }
}
Please confirm this is working for you.

Re: selectable element after last character in ListviewHeader

Posted: 20 Jun 2019, 19:34
by wyvern010
Did exactly as you said:
<GridViewColumn Header="First name">
 <ListView Grid.Row="2" ItemsSource="{Binding cbPersons}" MouseDoubleClick="ListView_MouseDoubleClick" ButtonBase.Click="GridViewColumnHeader_Click" SelectedItem="{Binding SelectedPerson}">
 
  private void GridViewColumnHeader_Click(object sender, RoutedEventArgs e)
        {
    (breakpoint not hit)        GridViewColumnHeader column = (sender as GridViewColumnHeader);
        }
            

The GridViewColumnHeader_Click get assigned, but not called.
I've put a breakpoint on the first line into the function, which didn't hit.

Re: selectable element after last character in ListviewHeader

Posted: 21 Jun 2019, 17:46
by sfernandez
I suppose you have overriden the ConnectEvent method in the code-behind class:
    protected override bool ConnectEvent(object source, string eventName, string handlerName)
    {
        if (eventName == "Click" && handlerName == "GridViewColumnHeader_Click")
        {
            ((UIElement)source).AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(GridViewColumnHeader_Click));
            return true;
        }
        //...
        return false;
    }
Without that piece of code event handlers won't be registered so will never be called.

Re: selectable element after last character in ListviewHeader

Posted: 22 Jun 2019, 13:21
by wyvern010
I did hookup it wrong, now the GridViewColumnHeader_Click(object sender, RoutedEventArgs e) gets called, but the sender is always ListView.

all code in
 if (sender is GridViewColumnHeader) { .. }
never gets executed.

Edit:
 if (sender is GridViewColumnHeader) { .. }

must be
 if (e.Source is GridViewColumnHeader) { .. }

Now works as expected!

Side effect is that i can't use the ColumnHeader.Tag to pass data to the Sorting function.

Re: selectable element after last character in ListviewHeader

Posted: 25 Jun 2019, 10:11
by sfernandez
You're right, it should be e.Source, the object who initially fired the event.
To help with the sorting, can't you use the Column info of the GridViewColumnHeader?