Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Data
Data Binding
 Sort and Group Data Using a View in...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Windows Presentation Foundation
How to: Sort and Group Data Using a View in XAML

This example shows how to create a view of a data collection in Extensible Application Markup Language (XAML). Views allow for the functionalities of grouping, sorting, filtering, and the notion of a current item.

In the following example, the static resource named places is defined as a collection of Place objects, in which each Place object is consisted of a city name and the state. The prefix src is mapped to the namespace where the data source Places is defined. The prefix scm and dat are mapped to the System.ComponentModel and System.Windows.Data namespaces respectively.

The following example creates a view of the data collection that is sorted by the city name and grouped by the state.

XAML
<Window.Resources>

  <src:Places x:Key="places"/>

  <CollectionViewSource Source="{StaticResource places}" x:Key="cvs">
    <CollectionViewSource.SortDescriptions>
      <scm:SortDescription PropertyName="CityName"/>
    </CollectionViewSource.SortDescriptions>
    <CollectionViewSource.GroupDescriptions>
      <dat:PropertyGroupDescription PropertyName="State"/>
    </CollectionViewSource.GroupDescriptions>
  </CollectionViewSource>

The view can then be a binding source, as in the following example:

XAML
<ListBox ItemsSource="{Binding Source={StaticResource cvs}}"
         DisplayMemberPath="CityName" Name="lb">
  <ListBox.GroupStyle>
    <x:Static Member="GroupStyle.Default"/>
  </ListBox.GroupStyle>
</ListBox>

For bindings to XML data defined in an XmlDataProvider resource, precede the XML name with an @ symbol.

XAML
<XmlDataProvider x:Key="myTasks" XPath="Tasks/Task">
    <x:XData>
        <Tasks xmlns="">
            <Task Name="Groceries" Priority="2" Type="Home">
XAML
<CollectionViewSource x:Key="mySortedTasks"
                      Source="{StaticResource myTasks}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="@Priority" />
    </CollectionViewSource.SortDescriptions>
    <CollectionViewSource.GroupDescriptions>
        <dat:PropertyGroupDescription PropertyName="@Priority" />
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

To see the entire example, please see Sorting and Grouping Data in XAML Sample.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Will sorting logic be re-applied as the underlying data changes?      Drew Noakes   |   Edit   |   Show History
Assuming the items within a sorted collection implement INotifyPropertyChanged, would the collection view source automatically re-sort them as the values upon which they are sorted change? I haven't been able to get this working using ListCollectionView and have found no information regarding this on MSDN so far. It seems like a distinction worth documenting on this page.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker