2016-09-29 21 views
0

我有一个名为“Items”的基础ObservableCollection,它可以通过它的抽象父对象拥有两种类型的实例(peopleVM,messageVM)。如何为每种类型应用不同的排序说明?具有更多数据类型的CollectionView排序集合

这是我的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 

它产生的情侣BindingExpression在VS的输出,因为PeopleVM没有财产的DateTime等等...

我需要这样的:

--header peoples- //按名称升序
人1
人2

--header messages-- //按消息日期排序
消息1
消息2
消息3

+0

你是说集合将同时包含这两种类型的实例,或者有时它是全部的,有时是所有其他的? –

+0

是的,两种类型一起,具有不同的DataTemplates ...这是如何解决我所知道的这个问题的唯一方法。 –

+1

正确的是,不同的DataTemplates是正确的。但是你不能为每种类型提供不同的排序描述。一种选择是使用这两个类的属性的联合集编写一个接口,并让它们都实现它。每个类都会为不支持的属性返回常量 - 比如messageVM中有一个date属性,而不是在peopleVM中;如果用户按日期排序,peopleVM会返回'DateTime.MaxValue'来将它们放在排序顺序的末尾。 –

回答

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属性添加到仅用于排序目的的ISearchedItem的实现中,这些属性将返回null | DateTime.MaxValue来摆脱BindingExpression错误,对我来说看起来很丑,但还没有找到更好的解决方案。