SortDescriptionCollection is reset when using CustomSort

浪子不回头ぞ 提交于 2020-01-05 08:55:29

问题


This is my first time ever (literally) using C# and WPF. So I am hoping the issue I have is something basic and simple that I am just not aware of.

Is there any way for my ListCollectionView to preserve its SortDescriptions after the CustomSort?

Here is the original code that has the behaviour that I need:

private void OnSorting(object sender, DataGridSortingEventArgs e)
{
    e.Handled = true;
    DataGridColumn column = e.Column;
    var direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;
    column.SortDirection = direction;
    var sortedColumnDescription = new SortDescription(e.Column.SortMemberPath, direction);

    var lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(((DataGrid)sender).ItemsSource);
    Console.WriteLine("Before sorting, SortDescriptions count is: " + lcv.SortDescriptions.Count);

    if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
    {
        lcv.SortDescriptions.Add(sortedColumnDescription);
    }
    else
    {
        lcv.SortDescriptions.Clear();
        lcv.SortDescriptions.Add(sortedColumnDescription);
    }
    Console.WriteLine("After sorting, SortDescription count is: " + lcv.SortDescriptions.Count);
}

Basically, in this case if the user pressed shift to do multi-column sort, it would add the sortedColumnDescription to SortDescriptions, and if I check the SortDescriptions count it would be equal to the amount of columns that I am sorting on.

The output is as follows:

// Single click a column
Before sorting, SortDescriptions count is: 0
After sorting, SortDescription count is: 1
// Shift + click columns from this point on
Before sorting, SortDescriptions count is: 1
After sorting, SortDescription count is: 2
Before sorting, SortDescriptions count is: 2
After sorting, SortDescription count is: 3

However I needed to implement a CustomSort, so all I did was add a single line after my else statement:

lcv.CustomSort = new IntegerSorter(lcv.SortDescriptions);

The goal was that I pass the SortDescriptions collection to my CustomSort and it figures out how to handle it all. But for some reason, just that one single line now gives me the following output:

// Single click a column
Before sorting, SortDescriptions count is: 0
After sorting, SortDescription count is: 0
// Shift + click columns from this point on
Before sorting, SortDescriptions count is: 0
After sorting, SortDescription count is: 0
Before sorting, SortDescriptions count is: 0
After sorting, SortDescription count is: 0

I tried storing SortDescriptions and then re-adding them after the CustomSort, but that triggers the default sorting behaviour on the listcollectionview which destroys the custom sorting I've done.

Thank you


回答1:


SortDescriptionCollection is reset when using CustomSort

This is the expected behaviour. Setting the CustomSort property clears a previously set SortDescriptions value. The documentation is pretty clear on this: https://msdn.microsoft.com/en-us/library/system.windows.data.listcollectionview.customsort(v=vs.110).aspx.

Is there any way for my ListCollectionView to preserve its SortDescriptions after the CustomSort?

No, you use either a custom sorter or the built-in one using SortDescriptions. You can't combine them.



来源:https://stackoverflow.com/questions/47534311/sortdescriptioncollection-is-reset-when-using-customsort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!