Search code examples
datagridwindows-community-toolkitwinui-3

WinUI3: How to sort datagrid groups?


I'm using the Windows Community Toolkit Datagrid.

I am binding the the datagrid's ItemSource to a grouped CollectionViewSource.

<ctWinUI:DataGrid
    ItemsSource="{x:Bind MyCollectioViewSourceView, Mode=OneWay}"

By default groups are sorted alphabetically:

alphabetically sorted groups

How can I implement custom sorting?

(Note I know how to sort datagrid rows, this is specifically for sorting datagrid groups.)

I have seen similar questions for the WPF datagrid, but the WinUI datagrid is different.


Solution

  • Just order the Source of the grouped MyCollectioViewSourceView, e.g.:

    CollectionViewSource cvs= new CollectionViewSource();
    cvs.IsSourceGrouped = true;
    cvs.Source = groupedItems.OrderByDescending(x => x.Key).ToList();