如何在WPF中订购组

时间:2021-09-21 20:10:57

In WPF, the CollectionViewSource allows for sorting (SortDescriptions) and grouping (GroupDescriptions). However, I can't find a way to order the groups. Is it possible?

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

2 个解决方案

#1


21  

<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>

Just add two SortDescriptions.Adding two sort descriptions allows us to sort the groups first and then the items within the groups.

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

For more check here

有关详情,请点击此处

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

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

#2


4  

Groups sorting is possible though it's not so straightforward. I'll explain it on the example.

虽然不是那么简单,但可以进行组排序。我会在这个例子上解释一下。

class CollectionElement
{
     public string Name {get; set; }
     public string Group {get; set; }
}

If you wish to group elements and sort the groups alphabetically then sort elements within each group alphabetically then you should do the following:

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

  1. Add PropertyGroupDescription referencing Group property
  2. 添加引用组属性的PropertyGroupDescription
  3. Add SortDescription referencing Group
  4. 添加引用组的SortDescription
  5. Add SortDescription referencing Name
  6. 添加引用名称的SortDescription

The grouping process seems to work effectively like the following way: Iterate through already sorted elements consequently. When encountering element form unknown group - create a group and add it to groups list. When encountering element from existing group - add it to the existing group. (Actual implementation may be different). So if your elements are sorted in the order you wish your groups to appear you will effectively sort the groups.

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

#1


21  

<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>

Just add two SortDescriptions.Adding two sort descriptions allows us to sort the groups first and then the items within the groups.

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

For more check here

有关详情,请点击此处

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

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

#2


4  

Groups sorting is possible though it's not so straightforward. I'll explain it on the example.

虽然不是那么简单,但可以进行组排序。我会在这个例子上解释一下。

class CollectionElement
{
     public string Name {get; set; }
     public string Group {get; set; }
}

If you wish to group elements and sort the groups alphabetically then sort elements within each group alphabetically then you should do the following:

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

  1. Add PropertyGroupDescription referencing Group property
  2. 添加引用组属性的PropertyGroupDescription
  3. Add SortDescription referencing Group
  4. 添加引用组的SortDescription
  5. Add SortDescription referencing Name
  6. 添加引用名称的SortDescription

The grouping process seems to work effectively like the following way: Iterate through already sorted elements consequently. When encountering element form unknown group - create a group and add it to groups list. When encountering element from existing group - add it to the existing group. (Actual implementation may be different). So if your elements are sorted in the order you wish your groups to appear you will effectively sort the groups.

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