集合视图源中的特定排序分组项

本文关键字:排序 视图 集合 | 更新日期: 2023-09-27 18:37:21

我有一个项目的集合视图源。每个项目都是一个对象,该对象具有一个属性 SentTime 和另一个属性 ParentId。我想按其父 ID 对项目进行分组,并按以下方式对它们进行排序:

  • 组中的项目应按 SentTime 升序排序
  • 组应按 SentTime 降序排序

下面是一个简短的示例:

  • 项目 1 已发送 1/10,父 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

我在视图模型中有我的集合视图源代码。

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));

这就是您可以实现排序应仅应用于每个组的方式。