如何在WPF中订购组

use*_*719 15 data-binding wpf

在WPF中,CollectionViewSource允许排序(SortDescriptions)和分组(GroupDescriptions).但是,我找不到订购这些团体的方法.可能吗?

bij*_*iju 22

<CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource animals}, Path=AnimalList}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="Category"/>
    </CollectionViewSource.GroupDescriptions>
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="Category" />
        <scm:SortDescription PropertyName="Name" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
Run Code Online (Sandbox Code Playgroud)

只需添加两个SortDescriptions.Adding两个排序描述允许我们先对组进行排序,然后对组内的项进行排序.

有关详情,请点击此处

http://bea.stollnitz.com/blog/?p=17


alp*_*use 5

分组排序是可能的,尽管它不是那么简单。我将在示例中进行解释。

class CollectionElement
{
     public string Name {get; set; }
     public string Group {get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如果希望对元素进行分组并按字母顺序对组进行排序,然后对每个组中的元素按字母顺序进行排序,则应执行以下操作:

  1. 添加PropertyGroupDescription引用Group属性
  2. 添加SortDescription引用Group
  3. 添加SortDescription引用 Name

分组过程似乎可以像以下方式一样有效地工作:因此,对已经排序的元素进行迭代。当遇到元素形式未知的组时-创建一个组并将其添加到组列表中。遇到现有组中的元素时-将其添加到现有组中。(实际实现可能有所不同)。因此,如果您按希望分组显示的顺序对元素进行排序,则可以有效地对分组进行排序。