Page 1 of 1

Data sorting with columns in Unity

Posted: 31 Jul 2018, 16:30
by nyna77
Hi,
I'm trying to make table with data that can be filtered and sorted by some values. I ended up with ListView and GridView as View for that List. To allow user to sort rows by values in columns I added ColumHeader with call to appropriate method in CodeBehind. It's works pretty good in Visual Studio as XAML project. XAML code below.
<ListView Grid.ColumnSpan="5" Grid.Row="2" ItemsSource="{Binding FilteredApartament}">
                <ListView.View>
                    <GridView AllowsColumnReorder="False">
                        <GridViewColumn DisplayMemberBinding="{Binding Path=ID}" Width="100">
                            <GridViewColumnHeader Content="ID" Click="Sort" />
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Floor}" Width="100">
                            <GridViewColumnHeader Content="Floor" Click="Sort" />
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
But when I tried to run it in Unity I met same complications.
1.
protected override bool ConnectEvent(object source, string eventName, string handlerName)
        {
            if (eventName == "Click" && handlerName == "Sort")
            {
                ((GridViewColumnHeader)source).Click += this.Sort;
                return true;
            }
            return false;
        }
Code above subscribe event but don't call assigned method on actually click. Is it my miss understand of this system or bug?

2. I worked around above with:
protected override bool ConnectEvent(object source, string eventName, string handlerName)
        {
            if (eventName == "Click" && handlerName == "Sort")
            {
                var g = (GridViewColumnHeader)source;
                g.MouseDown += this.Sort;
                g.Click += this.Sort;
                return true;
            }

            return false;
        }
but then in my sorting method after cast sender to GridViewColumnHeader the Column property is null. I need this for:
columnHeaderClicked.Column.HeaderTemplate = direction == SortState.Ascending ?
                Resources["HeaderTemplateArrowUp"] as DataTemplate :
                Resources["HeaderTemplateArrowDown"] as DataTemplate;
Are those knows issues? Is there any better way to do that?

TIA

Re: Data sorting with columns in Unity

Posted: 01 Aug 2018, 13:58
by sfernandez
Hi,

I reviewed the code and found we have a bug there. Because we are always creating a GridViewColumnHeader instance to use as Content what is defined in GridViewColumn.Header. So what is happening is that Click is raised for that internally generated object instead of the one defined in xaml.

Could you please report this in our bugtracker so we can keep track of the problem, thanks.

Meanwhile the solution will be quite tricky, because you would need to search (on the Loaded event) for the GridViewHeaderRowPresenter inside the ListView template, and get the generated GridViewColumnHeader by iterating its visual children. Then you can attach the Click handler to the correct headers. Let me know if you need help with that.

Re: Data sorting with columns in Unity

Posted: 18 Jun 2019, 15:20
by wyvern010
Hi i can also confirm this bug inside C# (No Unity)
Click action will bind but not be called.
MouseLeftButtonDown does in fact get called, but Column property is empty.

Re: Data sorting with columns in Unity

Posted: 19 Jun 2019, 19:40
by sfernandez
I created a ticket to follow the resolution of this bug: https://www.noesisengine.com/bugs/view.php?id=1499