Custom sorting logic for groups using CollectionViewSource

I have a list of "material" that needs to be filtered and then displayed in a grouped and sorted way. The data needed to calculate the grouping and sorting order is not available as simple properties - to calculate the order and groups, you need to do some work in the code.

CollectionViewSource allows me to define a custom filter and sorting logic - so far so good. It also allows you to bind GroupDescriptions to a value converter so that I can generate group names.

The last thing I want to do is to control the order in which the generated groups appear, and it hurts me!

Everything I see about CollectionViewSource.SortDescriptions says that it will sort the groups by property name, but I do not have the properties available for sorting. SortDescriptions cannot be attached to a value converter like GroupDescriptions, and I am not getting into other ideas.

So - how do you implement your own CollectionViewSource group sorting logic?

+4
source share
1 answer

This Bea Stollnitz blog post , and the GitHub repositories demonstrate how you can do this. First you need to sort by the criteria of your grouping. Even if this is not a specific property, it should be possible to sort your elements using the same logic that you use to group them, right ?! Of course, this is not possible when using an instance of SortDescription, but instead you can use the ListCollectionView.CustomSort property and specify the appropriate implementation of IComparer .

+6
source

Source: https://habr.com/ru/post/1309394/


All Articles