In my previous post we were introduced to the CollectionViewSource. We have multiple ItemSources binding to the same collection using a CollectionViewSource – very powerful. Now what if we want to sort the items displayed in one of our grid cells? We will focus on row one and column one for the rest of this post.
I’ve added some more default data to the Control’s constructor to make things a little more interesting and also notice I have added a few buttons to the bottom of our control:
The Sort by Name button adds a sort description to the CollectionViewSource
theDataContext[0].Column1.View.SortDescriptions.Add(new SortDescription("SomeName",ListSortDirection.Ascending));
This code tells the CollectionViewSource to sort the items in it’s view by the SomeName property in ascending order. Click the button and we see the following:
Click the Clear Sort Descriptions button.
The Sort By Value button tells the CollectionViewSource to sort the items in the view by the SomeValue Property in ascending order.
theDataContext[0].Column1.View.SortDescriptions.Add(new SortDescription("SomeValue", ListSortDirection.Ascending));
Click the button to see the following:
This time our list is sorted by the SomeValue property.
Where things get interesting is that you can add multiple sort descriptions. Try clearing the filter, then adding the name and value filter (in that order) and we see the following:
The collection is first sorted by name, then by value.
Clear the filter and click the Value, then name button and we see this:
The collection is first sorted by SomeValue, then by SomeName.
The order is important! If you want to add a sort description to the top of the sort order, simply use the Insert method as follows:
theDataContext[0].Column1.View.SortDescriptions.Insert(0,new SortDescription("SomeValue", ListSortDirection.Ascending));
Here is the modified XAML:
- <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightApplication1.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"> <UserControl.Resources> <Style TargetType="Button"> <Setter Property="FontSize" Value="9"/> </Style> </UserControl.Resources> <Grid x:Name="LayoutRoot"> <ScrollViewer VerticalScrollBarVisibility="Auto"> <StackPanel Orientation="Vertical"> <data:DataGrid x:Name="TheMainDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding}" HeadersVisibility="Column"> <data:DataGrid.Columns> <data:DataGridTemplateColumn Header="Column 1"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <ListBox MinHeight="15" ItemsSource="{Binding Path=Column1.View}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Margin="10" Text="{Binding Path=SomeName}" /> <TextBlock Margin="10" Text="{Binding SomeValue}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="Column 2"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <ListBox MinHeight="15" ItemsSource="{Binding Path=Column2.View}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Margin="10" Text="{Binding SomeName}" /> <TextBlock Margin="10" Text="{Binding SomeValue}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns> </data:DataGrid> <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"> <TextBlock Margin="5" Text="Name:"/> <TextBox x:Name="tbNAme" Margin="5" Width="100"></TextBox> <TextBlock Margin="5" Width="100" Text="Value:"/> <TextBox x:Name="tbValue" Margin="5" Width="100"></TextBox> <Button x:Name="AddButton" Margin="5" Content="Add" Click="AddButton_Click"/> </StackPanel> <StackPanel HorizontalAlignment="Left"> <Button x:Name="ChangeNameButton" Width="150" Content="Change Name" Click="ChangeNameButton_Click"/> <Button x:Name="ChangeValueButton" Width="150" Content="Change Value" Click="ChangeValueButton_Click"/> <Button x:Name="refresh" Width="150" Content="Refresh CVS" Click="refresh_Click" /> <Button x:Name="AddNameSort" Width="150" Content="Sort by Name" Click="AddNameSort_Click"/> <Button x:Name="AddValueSort" Width="150" Content="Sort by Value" Click="AddValueSort_Click"/> <Button x:Name="ClearSort" Width="150" Content="Clear Sort Descriptions" Click="ClearSort_Click"/> </StackPanel> </StackPanel> </ScrollViewer> </Grid>
- </ UserControl>
Here is the modified code behind:
- using System.Collections.ObjectModel;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.ComponentModel;
- namespace SilverlightApplication1
- {
- public partial class MainPage : UserControl
- {
- ObservableCollection<TestClass> theMainCollection ;
- ObservableCollection<TheRowClass> theDataContext;
- public MainPage()
- {
- InitializeComponent();
- theMainCollection = new ObservableCollection<TestClass>()
- { new TestClass() {SomeName="A",SomeValue=11},
- new TestClass() {SomeName="A",SomeValue=12},
- new TestClass() {SomeName="A",SomeValue=10},
- new TestClass() {SomeName="AB",SomeValue=21},
- new TestClass() {SomeName="A",SomeValue=13},
- new TestClass() {SomeName="B",SomeValue=13},
- new TestClass() {SomeName="B",SomeValue=14},
- new TestClass() {SomeName="AB",SomeValue=11},
- new TestClass() {SomeName="BC",SomeValue=22},
- new TestClass() {SomeName="AB",SomeValue=23},
- new TestClass() {SomeName="BC",SomeValue=24}
- };
- theDataContext = new ObservableCollection<TheRowClass>();
- theDataContext.Add(new TheRowClass(theMainCollection));
- theDataContext.Add(new TheRowClass(theMainCollection));
- TheMainDataGrid.DataContext = theDataContext;
- AddFilter(theDataContext[0].Column1, 1, 1);
- AddFilter(theDataContext[0].Column2, 1, 2);
- AddFilter(theDataContext[1].Column1, 2, 1);
- AddFilter(theDataContext[1].Column2, 2, 2);
- }
- private void AddFilter(CollectionViewSource cvs, int row, int column)
- {
- cvs.Filter += delegate(object o, FilterEventArgs eArgs)
- {
- //Simple filter saying if column = 1 look for an A in the SomeName property
- //Else look for a B. If we are in Row 1 look for a 1 in the SomeValue property
- //Else look for a 2
- string columnFilter;
- if (row == 1)
- columnFilter = "A";
- else
- columnFilter = "B";
- if ((eArgs.Item as TestClass).SomeValue.ToString().Contains(column.ToString()) && (eArgs.Item as TestClass).SomeName.Contains(columnFilter))
- eArgs.Accepted = true;
- else
- eArgs.Accepted = false;
- };
- }
- private void AddButton_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- TestClass tc = new TestClass();
- tc.SomeName = tbNAme.Text;
- tc.SomeValue = int.Parse(tbValue.Text);
- theMainCollection.Add(tc);
- }
- private void ChangeNameButton_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- theMainCollection[theMainCollection.Count-1].SomeName = "B";
- }
- private void ChangeValueButton_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- theMainCollection[theMainCollection.Count - 1].SomeValue = 25;
- }
- private void refresh_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- theDataContext[0].Column1.View.Refresh();
- theDataContext[1].Column1.View.Refresh();
- theDataContext[0].Column2.View.Refresh();
- theDataContext[1].Column2.View.Refresh();
- }
- private void AddNameSort_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- theDataContext[0].Column1.View.SortDescriptions.Add(new SortDescription("SomeName",ListSortDirection.Ascending));
- }
- private void AddValueSort_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- theDataContext[0].Column1.View.SortDescriptions.Add(new SortDescription("SomeValue", ListSortDirection.Ascending));
- }
- private void ClearSort_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- theDataContext[0].Column1.View.SortDescriptions.Clear();
- }
- }
- }
-
See my previous post to get the supporting code.