user3650732
user3650732

Reputation: 41

WPF DataGrid Multi Column Sort not working when overriding OnSorting

So I've got a plain vanilla DataGrid, and it's bound against an ICollectionView.

I handle the DataGrid's Sorting event, and set e.Handled to true, to suppress it's default behaviour, for some reason once I try to clear my ICollectionView's SortDescriptions, and add two sort descriptions in place, it does absolutely nothing, it does not honour the secondary sort at all. Here is my simple code:

    private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e)
    {
        DataGrid dataGrid = (DataGrid)sender;

        e.Handled = true;

        ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;


        var col = (DataContext as TeamModel).Players;

        col.SortDescriptions.Clear();
        col.SortDescriptions.Add(new SortDescription("Id", ListSortDirection.Ascending));
        col.SortDescriptions.Add(new SortDescription("Name", direction));
    }

I just want to simply have a primary and secondary sort, but it seems to be doing nothing.

Any ideas?

Upvotes: 0

Views: 526

Answers (1)

pushpraj
pushpraj

Reputation: 13669

You need to call .Refresh() of ICollectionView after you are finished editing the SortDescriptions

col.Refresh();

or alternatively you may use another method

using(col.DeferRefresh())
{
    col.SortDescriptions.Clear();
    col.SortDescriptions.Add(new SortDescription("Id", ListSortDirection.Ascending));
    col.SortDescriptions.Add(new SortDescription("Name", direction));
}

Upvotes: 0

Related Questions