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.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With