Declarative Sorting and Grouping

Setting up sorting and grouping characteristics in code is handy if you want to flip the characteristics programmatically, as we've been doing. However, if you've got a predetermined set of data massaging you'd like to do, a CollectionViewSource is a handy place to keep those settings (see Example 7-28).

Example 7-28. Declarative sorting and grouping <Window ...




<Window.Resources> <local:People x:Key="Family"> <local:Person Name="Tom" Age="ll" /> <local:Person Name="John" Age="12" /> <local:Person Name="Melissa" Age="38" /> <local:Person Name="Penny" Age="38" /> </local:People>

<local:AgeToRangeConverter x:Key="ageConverter" />

<CollectionViewSource x:Key="SortedGroupedFamily"

Source="{StaticResource Family}">

<CollectionViewSource.SortDescriptions> <compModel:SortDescription PropertyName="Name" Direction="Ascending" /> <compModel:SortDescription PropertyName="Age" Direction="Descending" /> </CollectionViewSource.SortDescriptions>

<CollectionViewSource.GroupDescriptions> <data:PropertyGroupDescription PropertyName="Age"

Converter="{StaticResource ageConverter}" /> <data:PropertyGroupDescription PropertyName="Age" /> </CollectionViewSource.GroupDescriptions> </CollectionViewSource>

</Window.Resources> <Grid> <ListBox

ItemsSource="{Binding Source={StaticResource SortedGroupedFamily}}"



<x:Static Member="GroupStyle.Default" /> </ListBox.GroupStyle>

In Example 7-28, we bring in the System.ComponentModel and System.Windows.Data namespaces first so that we can create SortDescription and PropertyGroupDescription objects. Then we create a CollectionViewSource object, which sorts and groups our data (provided via the Source property) and exposes an ICollectionView implementation.

Inside the CollectionViewSource, we set up the sorting and grouping policies we've been setting up programmatically. Notice the use of multiple group descriptors, including one that brings in a custom value converter, just like our most advanced grouping code sample.

Finally, we bind the listbox to the CollectionViewSource, so it can get the sorted and grouped data, as shown in Figure 7-17.

Fl ColiectionViewSourceBinding '=' ^ !

Under the Hill





Over the Hill




Figure 7-17. Declarative sorting and grouping in action

Unfortunately, this technique isn't quite as robust as the code-based technique; it doesn't allow custom sorting code, nor does it allow filtering of any kind. However, it lets us go quite a way without imperative code (excluding the custom value converter, of course).

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment