使用CollectionViewSource的组的自定义排序逻辑 [英] Custom sorting logic for groups using CollectionViewSource

查看:119
本文介绍了使用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让我定义自定义过滤器和排序逻辑-到目前为止,一切都很顺利.它还使我可以将GroupDescriptions绑定到值转换器,以便生成组名.

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!

我看到的关于 CollectionViewSource.SortDescriptions 的所有内容都表示它将按属性名称对组进行排序,但是我没有可用于排序的属性.不能像GroupDescriptions那样将SortDescriptions绑定到值转换器,而我没有其他想法.

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.

那么-您如何实现CollectionViewSource组的自定义排序逻辑?

So - how do you implement custom sorting logic of CollectionViewSource groups?

推荐答案

和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 appropriate IComparer implementation.

这篇关于使用CollectionViewSource的组的自定义排序逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆