开发者

Any idea on how to sort a CompositeCollection?

开发者 https://www.devze.com 2022-12-18 04:37 出处:网络
I have a CompositeCollection that consists of ObservableCollections of two types: Companies and Contacts.

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

StackOver开发者_如何学JAVAflow

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.


One can make use of IComparable and a interface for the object to be added into the collection. Then one can sort by

 IComparable getName()  // this should return company/employee name

in the compare method you can sort as per your requirement


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消