如何对CollectionViewSource的组进行排序

时间:2021-01-14 14:05:50

I have following collection view

我有以下收集视图

<CollectionViewSource x:Key="messages" Source="{Binding src}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="Group"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

Then I assign it to TreeView's ItemsSource. Now, how to sort Groups by their name? They appear to have random order.

然后我将它分配给TreeView的ItemsSource。现在,如何按他们的名字分类?它们似乎有随机的顺序。

1 个解决方案

#1


6  

Just sort by the group. This should work:

只是按组排序。这应该工作:

<CollectionViewSource x:Key="messages" Source="{Binding src}"> 
    <CollectionViewSource.GroupDescriptions> 
        <PropertyGroupDescription PropertyName="Group"/> 
    </CollectionViewSource.GroupDescriptions> 
    <CollectionViewSource.SortDescriptions>
        <SortDescription PropertyName="Group" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource> 

#1


6  

Just sort by the group. This should work:

只是按组排序。这应该工作:

<CollectionViewSource x:Key="messages" Source="{Binding src}"> 
    <CollectionViewSource.GroupDescriptions> 
        <PropertyGroupDescription PropertyName="Group"/> 
    </CollectionViewSource.GroupDescriptions> 
    <CollectionViewSource.SortDescriptions>
        <SortDescription PropertyName="Group" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>