I want to sort a WPF-Toolkit DataGrid on a column which is bound like this:
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="MyColumn" Binding="{Binding AnObject.AProperty}" />
</toolkit:DataGrid.Columns>
Now when AnObject is null, an ArgumentException with the Message "At least one object must implement IComparable" will be thrown.
What's the best way to work around this limitation? I don't really want to change the domain model to retur开发者_运维技巧n a NullObject-Pattern.
Using Convertors in combination with a binding directly to AnObject could turn out to be quite cumbersome, since I have many of those columns and more than one DataGrid.
Should I write a custom ViewModel foreach DataGrid and use a Converter on the ItemsSource Property?
I appreciate every suggestion!
I'd go with a different strategy: create a ViewModel for AnObject with a property that exposes AProperty and use it on all DataGrids. The VM should be capable of sending out the appropriate values on AProperty when AnObject is null.
Found the best solution here:
WPF Datagrid sort on column with null elements
Implemented a custom sorter for my DataGrids which convers null values.
精彩评论