具有更多数据类型的CollectionView排序集合

时间:2016-09-29 12:55:14

标签: c# wpf listview mvvm collectionview

我有一个基础的ObservableCollection,名为" Items"它可以通过其抽象父类保存两种类型的实例(peopleVM,messageVM)。如何为每种类型应用不同的排序描述?

这是我的CollectionViewSource:

var cvs = CollectionViewSource.GetDefaultView(Items);

PropertyGroupDescription groupDescriptionMessages = new PropertyGroupDescription(nameof(ISearchedItem.ItemType)); // Group by item type - peoples | messages
cvs.GroupDescriptions.Add(groupDescriptionMessages);

cvs.SortDescriptions.Add(new SortDescription(nameof(ISearchedItem.ItemType), ListSortDirection.Descending)); // Primary sort by Item type - peoples | messages
cvs.SortDescriptions.Add(new SortDescription(nameof(PeopleVM.ContactName), ListSortDirection.Ascending)); // Sort peoples by name
cvs.SortDescriptions.Add(new SortDescription(nameof(MessageVM.DateTime), ListSortDirection.Descending)); // Sort messages by date time

它在VS输出中产生了几个BindingExpression,因为PeopleVM没有属性DateTime等等......

我需要有类似的东西:

- 标题人 - //按名称升序排序
人1 人2

- 标题消息 - //按消息日期分类
消息1
消息2
消息3

1 个答案:

答案 0 :(得分:0)

我最终使用标准的SortDescription:

cvs.SortDescriptions.Add(new SortDescription(nameof(ISearchedItem.ContactName), ListSortDirection.Ascending)); // Sort peoples by name
cvs.SortDescriptions.Add(new SortDescription(nameof(ISearchedItem.DateTime), ListSortDirection.Descending)); // Sort messages by date time

这意味着我必须仅将getter only属性添加到ISearchedItem的实现中以用于排序目的,这些属性返回null | DateTime.MaxValue摆脱BindingExpression错误,对我来说似乎很难看,但还没有找到更好的解决方案。