Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Test case for sort issue in DataGrid

By: a guest on Aug 31st, 2010  |  syntax: XML  |  size: 2.12 KB  |  views: 61  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <Window x:Class="DataGridTest5.MainWindow"
  2.        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.        xmlns:local="clr-namespace:DataGridTest5"
  5.        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
  6.        Title="Test" Height="200" Width="650">
  7.   <Window.Resources>
  8.     <!-- DataGrid source, as a CollectionViewSource to allow for sorting and/or filtering -->
  9.     <CollectionViewSource x:Key="Players"
  10.                          Source="{Binding ElementName=lstLevel, Path=SelectedItem.Players}">
  11.       <CollectionViewSource.SortDescriptions>
  12.         <scm:SortDescription PropertyName="Name" />
  13.       </CollectionViewSource.SortDescriptions>
  14.     </CollectionViewSource>
  15.   </Window.Resources>
  16.   <Grid>
  17.     <Grid.ColumnDefinitions>
  18.       <ColumnDefinition Width="120" />
  19.       <ColumnDefinition Width="250" />
  20.       <ColumnDefinition Width="*" />
  21.     </Grid.ColumnDefinitions>
  22.  
  23.     <!-- Level selection -->
  24.     <ScrollViewer Grid.Column="0">
  25.       <ListBox Name="lstLevel"
  26.               DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True" />
  27.     </ScrollViewer>
  28.  
  29.     <!-- Players data, with sort on the Name column -->
  30.     <StackPanel Grid.Column="1">
  31.       <Label>DataGrid:</Label>
  32.       <DataGrid Name="lstPlayers" AutoGenerateColumns="False"
  33.                CanUserSortColumns="False"
  34.                ItemsSource="{Binding Source={StaticResource Players}}">
  35.         <DataGrid.Columns>
  36.           <DataGridTextColumn Header="Name"
  37.                              Binding="{Binding Path=Name, Mode=TwoWay}"
  38.                              Width="*" />
  39.           <DataGridTextColumn Header="Age"
  40.                              Binding="{Binding Path=Age, Mode=TwoWay}"
  41.                              Width="80">
  42.           </DataGridTextColumn>
  43.         </DataGrid.Columns>
  44.       </DataGrid>
  45.     </StackPanel>
  46.  
  47.     <StackPanel Grid.Column="2">
  48.       <Label>ListBox:</Label>
  49.       <ListBox ItemsSource="{Binding Source={StaticResource Players}}" DisplayMemberPath="Name" />
  50.     </StackPanel>
  51.   </Grid>
  52. </Window>