关于如何对 CompositeCollection 进行排序有什么想法吗?
我有一个 CompositeCollection,它由两种类型的 ObservableCollections 组成:公司和联系人。 联系人有一个属性 FullName,而公司有一个属性 Name。
我想应用排序,以便集合按类型混合,但按名称排序,例如:
Itzhak Perlman
约翰·多伊
微软
莎拉·摩尔
堆栈溢出
Walter Richardson
请注意,我将 ItemsControl 绑定到 CollectionViewSource,其 Source 设置为 CompositeCollection,它的视图变为 CompositeCollectionView,它是内部类型,如果我显式将其类型设置为 ListCollectionView,则它仅包含 CollectionContainers 作为其项目。
所需的解决方案是一种在 CompositeCollections 上启用排序、过滤和分组的解决方案,我不关心覆盖和创建我自己的 CompositeCollection,我只是不知道如何覆盖它并在 xaml 中启用其功能。
I have a CompositeCollection that consists of ObservableCollections of two types: Companies and Contacts.
Contact has a property FullName while Company has a property Name.
I want to apply sorting so the collections are mixed by their types but sorted by their name, example:
Itzhak Perlman
John Doe
Microsoft
Sarah Moore
StackOverflow
Walter Richardson
Note that I bound the ItemsControl to a CollectionViewSource that its Source is set the CompositeCollection, it's view becomes a CompositeCollectionView, it's an internal type, if I explicitly set its type to ListCollectionView, it only contains the CollectionContainers as its items.
Desired solution is a workaround to enable sorting, filtering and grouping on a CompositeCollections, I don't care to override and create my own CompositeCollection, I just don't know how I can override this and enable its functionality in xaml.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以利用 IComparable 和一个接口将对象添加到集合中。然后可以在比较方法中进行排序,
您可以根据您的要求进行排序
One can make use of
IComparable
and a interface for the object to be added into the collection. Then one can sort byin the compare method you can sort as per your requirement
谁说我必须利用“CompositeCollection”这个东西?
我决定不使用 CompositeCollection。
我只是创建一个混合了两种类型的 ObservableCollection(of Object),并将 CollectionViewSource 绑定到它。
更新
通过继承 CompositeCollection 并实现
IBindingList
,可以启用排序。Who said I must get advantage of the thingy called 'CompositeCollection'?
I decided not to use a CompositeCollection.
I am just creating an ObservableCollection(of Object) mixed of the two types, and binding the CollectionViewSource to it.
Update
By inheriting CompositeCollection and implenting
IBindingList
, sorting can be enabled.