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
你是说集合将同时包含这两种类型的实例,或者有时它是全部的,有时是所有其他的? –
是的,两种类型一起,具有不同的DataTemplates ...这是如何解决我所知道的这个问题的唯一方法。 –
正确的是,不同的DataTemplates是正确的。但是你不能为每种类型提供不同的排序描述。一种选择是使用这两个类的属性的联合集编写一个接口,并让它们都实现它。每个类都会为不支持的属性返回常量 - 比如messageVM中有一个date属性,而不是在peopleVM中;如果用户按日期排序,peopleVM会返回'DateTime.MaxValue'来将它们放在排序顺序的末尾。 –