c# - collectionviewsource 中的特定排序分组项目

标签 c# wpf sorting grouping collectionviewsource

我有一个 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/

相关文章:

c# - 在 WPF C# 中使用数据绑定(bind)更改组合框的选定索引

WPF ListView 垂直滚动条

具有某些规范的 C++ 数组排序

c - 使用链表排序

c# - Entity Framework 代码优先 : Is there a way to automatically filter disabled rows from DbSets?

c# - 在 WPF 应用程序中加载非托管 DLL

c# - Microsoft OWIN 测试服务器设置 cookie

wpf - 寻找带有复选框的 WPF ComboBox

在 MATLAB 中将项目分类到容器中

c# - 关闭应用程序时 WindowsBase.dll 中出现“System.Threading.Tasks.TaskCanceledException”