使用 CollectionViewSource 为组自定义排序逻辑
我有一个“东西”列表,需要过滤然后以分组和排序的方式显示。计算分组和排序顺序所需的数据不能作为简单属性提供 - 需要在代码中完成一些工作来计算顺序和组。
CollectionViewSource 让我可以定义自定义过滤器和排序逻辑 - 到目前为止一切顺利。它还允许我将 GroupDescriptions 绑定到值转换器,以便我可以生成组名称。
我想做的最后一件事是控制生成的组出现的顺序,这让我很痛苦!
我看到的关于 CollectionViewSource.SortDescriptions 的所有内容都表明它将按属性名称,但我没有可供排序的属性。 SortDescriptions 不能像 GroupDescriptions 那样绑定到值转换器,而且我没有其他想法。
那么 - 如何实现 CollectionViewSource 组的自定义排序逻辑?
I have a list of "stuff" which needs to be filtered then displayed in a grouped and sorted manner. The data needed to calculate the grouping and sorting order is not available as simple properties - there needs to be some work done in code to calculate the order and groups.
CollectionViewSource lets me define custom filter and sort logic - so far so good. It also lets me bind GroupDescriptions to a value converter so that I can generate the group names.
The last thing I want to do is control the order that the generated groups appear and this is causing me pain!
Everything I see about CollectionViewSource.SortDescriptions says that it will sort groups by a property name, but I don't have a property available to sort by. SortDescriptions can't be bound to a value converter like GroupDescriptions can and I'm out of other ideas.
So - how do you implement custom sorting logic of CollectionViewSource groups?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Bea Stollnitz 博客上的这篇文章、和 GitHub 存储库、演示如何做到这一点。您必须首先按分组标准进行排序。即使这不是一个具体的属性,也应该可以使用与对项目进行分组相同的逻辑对项目进行排序,不是吗?当然,使用 SortDescription 实例是不可能的,但您可以使用
ListCollectionView.CustomSort
属性并指定适当的IComparer
实现。This post on Bea Stollnitz' blog, and the GitHub repo, demonstrates how you can do that. You will have to sort by the criteria of your grouping first. Even if this is not a concrete property, it should be possible to sort your items using the same logic that you use to group them, isn't it?! Certainly, this is not possible using a SortDescription instance, but you could instead use the
ListCollectionView.CustomSort
property and specify an appropriateIComparer
implementation.