Question

I have recently stumbled across an issue where the WPF ListView control seems to be restricting the ability to sort its items. Specifically, I am having a great deal of trouble trying to get a SortDescription to recognise nested properties (properties of properties).

For straight-forward sorting by propreties, the following line should do the job:

listView.Items.SortDescriptions.Add(new SortDescription("MyProperty",
    ListSortDirection.Ascending));

And indeed it works well for me. However, because the ItemSource for my ListView is a (strongly-typed) DataTable, some of the columns are bound to nested properties of the row (i.e. Row.OtherTableRow.SubProperty binding path style) - it's the way ADO.NET DataSets work for hierarchial databases.

What I would like to do is something like this:

listView.Items.SortDescriptions.Add(new SortDescription("MyProperty.SubProperty",
    ListSortDirection.Ascending));

But unfortunately that line throws an ArgumentException:

'Orders' type does not have property named 'Row.[ID]', so cannot sort data collection.

Strangely enough, I have no problem binding to nested properties. Indeed, the fields display perfectly well in the ListView... Nor does adding a PropertyGroupDescription to listView.GroupDescriptions give me any problems with nested properties - it is only SortDescriptions!

Is this but a limitation of WPF/the ListView control? Is there any way I'm going to get support as part of the framework, or will I be out of luck there? If I unfortunately am, and this inconsistency is unavoidable, I would much appreciate if someone could suggest a hack or workaround for this scenario of nested properties. I have tried a few ideas already, but all with minimal success.

Note: I realise that an ORM may well solve my problems, but I'm afraid this isn't at all feasible for the current project I'm working on. Simple ADO.NET DataSets will have to do.

Was it helpful?

Solution

According to Windows Presentation Foundation Unleased (Adam Nathan), you can cast the default view to a ListCollectionView and set a custom IComparer implementation to its CustomSort property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top