我有一个 collectionviewsource 项目。每个项目都是一个对象,它有一个属性 SentTime 和另一个属性 ParentId。 我想按 ParentId 对项目进行分组,并按以下方式对它们进行排序:
- 组中的项目应按发送时间升序排序
- 组应按 SentTime 降序排序
这是一个简短的例子:
- Item 1 Sent 1/10,parent Id 1
- 项目 2 已发送 2/10,父 ID 1
- 项目 3 已发送 3/10,父 ID 3
- 项目 4 已发送 4/10,父 ID 3
- 项目 5 已发送 5/10,父 ID 1
那么分组列表应该是这样的:
项目 3
- 第 4 项
项目 1
- 第 2 项
- 第 5 项
我在 ViewModel 中有我的 collectionviewsource。
this.MyCvs.SortDescriptions.Clear();
this.MyCvs.SortDescriptions.Add(new SortDescription("SendTime", ListSortDirection.Ascending));
this.MyCvs.GroupDescriptions.Add(new PropertyGroupDescription("ParentId"));
我知道,要在 CVS 中订购组,我只需要通过属性添加另一个 SortDescription,项目根据该属性分组(在我的例子中是 ParentId),但它没有给出我想要获得的结果。 提前感谢大家的帮助。
最佳答案
当您在网格中进行分组和排序时,您必须将排序描述设置得与之前略有不同。
this.MyCvs.SortDescriptions.Clear();
this.MyCvs.GroupDescriptions.Add(new PropertyGroupDescription("ParentId"));
this.FilteredMessages.SortDescriptions.Add(new SortDescription("ParentId", ListSortDirection.Ascending));
this.FilteredMessages.SortDescriptions.Add(new SortDescription("SendTime", ListSortDirection.Ascending));
这就是您如何实现排序只适用于每个组。
关于c# - collectionviewsource 中的特定排序分组项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19675493/