开发者

How can I order Groups in WPF

In WPF, the CollectionViewSource allows for sorting (SortDescriptions) and grouping (GroupDescriptions). However, I can't f开发者_运维问答ind a way to order the groups. Is it possible?


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

For more check here

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


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. Add SortDescription referencing Group
  3. Add SortDescription referencing Name

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜