Search code examples
c#wpfdatagridicollectionview

WPF ICollectionView Group Order and Defaults


I have a collection of items that I am displaying in a DataGrid in WPF.

These items have the property Status, which can either be Approved, Pending, Draft or Archived.

I have set up an ICollectionView as follows:

formView = CollectionViewSource.GetDefaultView(Forms);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("Status");
formView.GroupDescriptions.Add(groupDescription);

I then bind this view to the DataGrid and it correctly groups and display my items.

My issue, is I need Archived to be at the bottom of the list, and collapsed by default however my binding automatically puts it before the other status. How can I achieve this?

Thanks.


Solution

  • You could either sort the source collection:

    var formView = CollectionViewSource.GetDefaultView(Forms.OrderBy(x =>
    {
        switch (x.Status)
        {
            case "Archived":
                return 1;
            default:
                return 0;
        }
    }));
    

    Or the CollectionView:

    CollectionViewSource with custom sort