Compartilhar via


Como: Ordenar e Agrupar Dados usando um modo de exibição em XAML

Atualização: July 2010

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.

Exemplo

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. O prefixo scm mapeia para "clr-namespace:System.ComponentModel;assembly=WindowsBase" e dat mapeia para "clr-namespace:System.Windows.Data;assembly=PresentationFramework".

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

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

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

Para ligações de dados XML, definidos em um XmlDataProvider recurso, preceda o nome do XML com um @ símbolo.

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

Consulte também

Tarefas

Como: Obter o Modo de Exibição Padrão de uma Coleção de Dados

Referência

CollectionViewSource

Conceitos

Revisão de Associação de Dados

Outros recursos

Data Binding How-to Topics

Histórico de alterações

Date

History

Motivo

July 2010

Adicionadas as seqüências de caracteres literais para o scm e dat prefixos.

Comentários do cliente.