2
votes

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 return 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!

2

2 Answers

1
votes

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.

0
votes

Found the best solution here:

WPF Datagrid sort on column with null elements

Implemented a custom sorter for my DataGrids which convers null values.