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:
We can create a custom
CollectionView
for theItems
collection: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 should also note the following:
So, using the
CollectionViewSource
, you're just defining a custom view for your collection.