tags:

views:

923

answers:

4

Hi. I have a WPF ListView with a column that has dates in it. I would like a way to custom sort the dates (by date value, rather than string value). Is there a way to do this? Right now I am using list.Items.SortDescriptions to sort using another column, but I would like to change this to sort on the date column instead.

Thanks.

A: 

Use ObservableList and a custom object type - and do the sorting yourself. Connect the ListView to the ObservableList using ItemsSource.

..., or you could try doing what this blog describes; Sorting ListViews

Thies
+1  A: 

Have you tried that ?

ICollectionView view = CollectionViewSource.GetDefaultView(listView.ItemsSource);
view.SortDescriptions.Add(new SortDescription("Date", ListSortDirection.Descending));

EDIT: You can also have a look at this attached property

Thomas Levesque
Yes I see that now. Maybe I was afraid it would just do string comparison. I made a property that converted my string date to a date object and it's working well. Thanks.
Max
A: 

If you're finding that the way WPF is sorting by default isn't what you intended, you can provide a custom sorter to the default view:

ListCollectionView view = 
  (ListCollectionView)CollectionViewSource.GetDefaultView(listView.ItemsSource);
view.CustomSort = new DateValueSorter();
listView.Items.Refresh();

Where DateValueSorter is a class that implements the IComparer interface and sorts according to the date value and produces the ordering you are looking for.

Nicholas Armstrong