I have a DataGrid, with columns XAML as such:
<DataGridTextColumn Header="Time" Binding="{Binding Date, StringFormat='yyyy-MM-dd HH:mm:ss'}" SortMemberPath="Date" SortDirection="Descending" Width="130" CanUserResize="True" />
<DataGridTextColumn Header="Level" Binding="{Binding Level}" Width="60" CanUserResize="True" />
<DataGridTextColumn Header="Source" Binding="{Binding Logger}" Width="150" CanUserResize="True" />
<DataGridTextColumn Header="Message" Binding="{Binding Message}" Width="*" CanUserResize="True" />
I bind this to an ObservableCollection<EalsLogEvent>
, where EalsLogEvent.Date
is typed DateTime
:
public ObservableCollection<EalsLogEvent> LogEvents
{
get
{
return _logEvents;
}
}
The grid viewmodel uses a timer to refresh itself, and everything seems fine with the grid, except when it first loads, on app startup. Then, the Time
column appears to be sorted descending, but is sorted ascending.
To get the sort right, I must click the column header twice; the first time changes the order to ascending, which now matches the content of the column. The second click on the column header changes its sort order back to descending, and this time it sorts the column contents properly, i.e. descending.
If I use LINQ to order the collection when _logEvents
gets refreshed, I lose whichever order the user had set for the column by clicking its header. If I have to have the view tell the model which order the LINQ sort should use, something smells bad.
You could use a CollectionViewSource
in your XAML to define the default sorting.
Assuming we have a view model:
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<Item> Items { get; private set; }
}
We can create a custom CollectionView
for the Items
collection:
<Window xmlns:l="clr-namespace:YourNamespace"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase">
<Window.DataContext>
<l:ViewModel/>
</Window.DataContext>
<Window.Resources>
<CollectionViewSource Source="{Binding Items}" x:Key="GridItems">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Date" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<DataGrid ItemsSource="{Binding Source={StaticResource GridItems}}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Time" Binding="{Binding Date, StringFormat='yyyy-MM-dd HH:mm:ss'}" Width="130" CanUserResize="True" />
<DataGridTextColumn Header="Level" Binding="{Binding Level}" Width="60" CanUserResize="True" />
<DataGridTextColumn Header="Source" Binding="{Binding Logger}" Width="150" CanUserResize="True" />
<DataGridTextColumn Header="Message" Binding="{Binding Message}" Width="*" CanUserResize="True" />
</DataGrid.Columns>
</DataGrid>
</Window>
With this approach, your underlying source collection (Items
in this example) will not be affected, the sorting occurs only in the view.
As you can read in MSDN:
You can think of a collection view as the layer on top of the binding source collection that allows you to navigate and display the collection based on sort, filter, and group queries, all without having to manipulate the underlying source collection itself. If the source collection implements the INotifyCollectionChanged interface, the changes raised by the CollectionChanged event are propagated to the views.
You should also note the following:
All collections have a default CollectionView. WPF always binds to a view rather than a collection. If you bind directly to a collection, WPF actually binds to the default view for that collection.
So, using the CollectionViewSource
, you're just defining a custom view for your collection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With