listview自定义排序与谓词(sortpropery是枚举)

时间:2013-07-08 12:58:13

标签: c# wpf listview sorting

此代码效果很好

 ICollectionView dataView = CollectionViewSource.GetDefaultView(this.ItemsSource);

            if (dataView != null)
            {
                // Specify the new sorting information.
                dataView.SortDescriptions.Clear();
                var description = new SortDescription(propertyName, _sortDirection);
                dataView.SortDescriptions.Add(description);

                dataView.Refresh();               
            }

并且对于exaple字符串类型的按字母顺序排序,并且对于枚举它在枚举数字的基础上排序,问题是我想要枚举的自定义比较。

1 个答案:

答案 0 :(得分:1)

如果您使用源列表实现IList,您可以执行的操作,您可以将集合视图源转换为ListCollectionView并将ListCollectionView.CustomSort设置为自定义IComparer类,您可以实现自定义排序逻辑

ListCollectionView dataView = (ListCollectionView)(CollectionViewSource.GetDefaultView(this.ItemsSource));
dataView.CustomSort = new MyCustomSort();