C# 如何排序CollectionViewSource';s组

C# 如何排序CollectionViewSource';s组,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我有以下收藏观点 <CollectionViewSource x:Key="messages" Source="{Binding src}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="Group"/> </CollectionViewSource.GroupDescriptions> </C

我有以下收藏观点

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


然后我把它分配给TreeView的ItemsSource。现在,如何按组名对组进行排序?它们似乎有随机顺序。

只需按组排序即可。这应该起作用:

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


您知道如何在代码中实现这一点吗?我的目的是在单击升序/降序按钮时对网格中的现有组进行排序。现在的情况是,我能够按字段名对行进行排序,但不能按groups.TWood-messages.SortDescriptions.Add(new-SortDescription(“Group”,ListSortDirection.升序));以防有人和我有同样的问题:您可能需要在窗口定义中添加xmlns:scm=“clr namespace:System.ComponentModel;assembly=WindowsBase”,并使用“scm”作为SortDescription的命名空间。