Falcon
Falcon

Reputation: 3180

WPF Toolkit DataGrid: Sorting with Null Values

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!

Upvotes: 2

Views: 1563

Answers (2)

Falcon
Falcon

Reputation: 3180

Found the best solution here:

WPF Datagrid sort on column with null elements

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

Upvotes: 0

robertos
robertos

Reputation: 1796

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.

Upvotes: 1

Related Questions