CustomSort on multi-column ListCollectionView

489 Views Asked by At

Being new to C#, I just found out that CustomSort clears SortDescriptions, and now I am a bit stuck on how to allow custom multi-column sorting of my datagrid.

You can see my code in my previous question

The particular line that I am trying to figure out is this:

lcv.CustomSort = new IntegerSorter(lcv.SortDescriptions);

In my IntegerSort custom sort, I am checking if SortDescriptions contains more than one column, and if so - perform the multi-column sort accordingly. This however relies on the fact that each time the user does a shift+click on the column, it appends the sort description to the ListCollectionView. Which doesn't work, since it is reset after every custom sort I do.

Are there any known workarounds for this? What is the proper way of doing a multi-column sort with a custom sort?

Thank you very much.

1

There are 1 best solutions below

1
On BEST ANSWER

When you first create a custom sort, you give it the list of sort descriptions that the ListCollectionView has.

The next time the user shift clicks, if lcv.SortDescriptions is empty, lcv.CustomSort won't be null. It'll be the IntegerSort you gave it last time around, and it'll still have that list of SortDescriptions you passed to its constructor. So grab it:

var intSort = lcv.CustomSort as IntegerSort;

Make a new collection, and create a new IntegerSort with the new list.

If IntegerSort doesn't make the SortDescriptions available as a public property, make it so it does.

Or if you want to keep that private, write a new constructor for IntegerSort for this purpose:

public IntegerSort(IntegerSort oldSort, SortDecsription add, SortDecsription remove = null)
{
    //  Put the new collection together
}

Use like so:

lcv.CustomSort = new IntegerSort(lcv.CustomSort as IntegerSort, sortedColumnDescription);