使用CustomSort时,将重置SortDescriptionCollection [英] SortDescriptionCollection is reset when using CustomSort

查看:130
本文介绍了使用CustomSort时,将重置SortDescriptionCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次(从字面上看)使用C#和WPF.因此,我希望我遇到的问题是一些我根本不了解的基本而简单的问题.

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.

我的ListCollectionView是否可以在CustomSort之后保留其SortDescriptions?

这是具有我需要的行为的原始代码:

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);
}

基本上,在这种情况下,如果用户按shift键进行多列排序,则会将sortedColumnDescription添加到SortDescriptions中,并且如果我检查SortDescriptions计数,它将等于我在其上进行排序的列数.

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.

输出如下:

// 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

但是我需要实现一个CustomSort,所以我要做的就是在else语句之后添加一行:

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);

目标是将SortDescriptions集合传递给CustomSort,并弄清楚如何处理所有这些.但是由于某种原因,现在只有一行显示了以下输出:

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

我尝试存储SortDescriptions,然后在CustomSort之后重新添加它们,但是这会触发listcollectionview上的默认排序行为,这会破坏我所做的自定义排序.

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.

谢谢

推荐答案

使用CustomSort时,SortDescriptionCollection被重置

SortDescriptionCollection is reset when using CustomSort

这是预期的行为.设置CustomSort属性将清除先前设置的SortDescriptions值.该文档对此非常清楚: https://msdn.microsoft.com/zh-cn/library/system.windows.data.listcollectionview.customsort(v = vs.110).aspx .

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.

ListCollectionView之后,我的ListCollectionView有什么方法可以保留其SortDescriptions吗?

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

否,您可以使用 自定义排序器,也可以使用内置的SortDescriptions排序器.您无法将它们结合在一起.

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

这篇关于使用CustomSort时,将重置SortDescriptionCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆