What causes a WPF ListCollectionView that uses custom sorting to re-sort its items?
Posted
by Drew Noakes
on Stack Overflow
See other posts from Stack Overflow
or by Drew Noakes
Published on 2009-03-02T10:17:54Z
Indexed on
2010/05/26
13:51 UTC
Read the original article
Hit count: 316
Consider this code (type names genericised for the purposes of example):
// Bound to ListBox.ItemsSource
_items = new ObservableCollection<Item>();
// ...Items are added here ...
// Specify custom IComparer for this collection view
_itemsView = CollectionViewSource.GetDefaultView(_items)
((ListCollectionView)_itemsView).CustomSort = new ItemComparer();
When I set CustomSort
, the collection is sorted as I expect.
However I require the data to re-sort itself at runtime in response to the changing of the properties on Item
. The Item
class derives from INotifyPropertyChanged
and I know that the property fires correctly as my data template updates the values on screen, only the sorting logic is not being called.
I have also tried raising INotifyPropertyChanged.PropertyChanged
passing an empty string, to see if a generic notification would cause the sorting to be initiated. No bananas.
EDIT In response to Kent's suggestion I thought I'd point out that sorting the items using this has the same result, namely that the collection sorts once but does not re-sort as the data changes:
_itemsView.SortDescriptions.Add(
new SortDescription("PropertyName", ListSortDirection.Ascending));
© Stack Overflow or respective owner