I have a DataGrid that is data bound to a collection view source. If I bind the DataGrid to a List<T> I can automatically sort throught the columns of my DataGrid by clicking on the Column Header.
If bound to a CollectionViewSource the column headers still show up the indicates as the DataGrid would Sort, but it does not sort. How can I achieve the same functionality?
This is my DataGrid:
<DataGrid Grid.Row="1" SelectedItem="{Binding SelectedItem}"
SelectionMode="Single" SelectionUnit="FullRow" AutoGenerateColumns="False" ItemsSource="{Binding CurrentErrorsViewSource.View}"
CanUserSortColumns="True" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTemplateColumn CanUserResize="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl Template="{StaticResource ErrorRemoteControl}" Foreground="{StaticResource GlyphBrush}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{userInterface:Translation Description}" Binding="{Binding Path=(viewModels:ErrorItemViewModel.ErrorInformation).Description}" Width="Auto" />
<DataGridTextColumn Header="{userInterface:Translation Code}" Binding="{Binding Path=(viewModels:ErrorItemViewModel.ErrorCode)}" Width="Auto" />
</DataGrid.Columns>
</DataGrid>
You could handle the
Sorting
event of the DataGrid, and in code-behind create the appropiate SortDescription object and add them to your CollectionViewSource's SortDescriptions collection.