多列ListCollectionView上的CustomSort

时间:2017-11-28 15:51:53

标签: c# wpf sorting listcollectionview

刚接触C#,我发现CustomSort清除了SortDescriptions,现在我对如何允许自定义多列排序数据网格感到困惑。

您可以在my previous question

中查看我的代码

我想弄清楚的具体路线是:

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

在我的IntegerSort自定义排序中,我正在检查SortDescriptions是否包含多个列,如果是,则相应地执行多列排序。然而,这依赖于以下事实:每次用户执行shift +单击列时,它会将排序描述附加到ListCollectionView。哪个不起作用,因为它在每次自定义排序后重置。

是否有任何已知的解决方法?使用自定义排序进行多列排序的正确方法是什么?

非常感谢。

1 个答案:

答案 0 :(得分:0)

首次创建自定义排序时,可以为其提供ListCollectionView具有的排序说明列表。

下次用户转移点击次数时,如果lcv.SortDescriptions为空,lcv.CustomSort将不会null。它将是您上次给出的IntegerSort,并且它仍然会传递给您的构造函数的SortDescriptions列表。抓住它:

var intSort = lcv.CustomSort as IntegerSort;

创建一个新集合,并使用新列表创建一个新的IntegerSort

如果IntegerSort没有将SortDescriptions作为公共属性提供,请将其设置为公共属性。

或者如果你想保持私有,为此目的为IntegerSort编写一个新的构造函数:

public IntegerSort(IntegerSort oldSort, SortDecsription add, SortDecsription remove = null)
{
    //  Put the new collection together
}

像这样使用:

lcv.CustomSort = new IntegerSort(lcv.CustomSort as IntegerSort, sortedColumnDescription);
相关问题