Can a ListCollectionView with a SortDescription on it automatically update itself?
What I am trying to achieve is a sorted list, where the list is kept sorted, and I am notified of changes in the sorted order.
For example, if I create an observable collection of text blocks, then create a ListCollectionView on that collection, and set a SortDescription on it to sort by the Text property, I get, as expected a list of the text blocks in the collection in the correct order. If I add or remove one, then this works as expected, inserting in the correct position.
What I can't get to work is that when I change the Text of any of the items in the list, it does not get resorted into its new correct position. Is there any way to make this happen?
Thanks,
Alex
So I'd need to listen to the TextChanged events on all the elements in the collection, and force the collection to resort by removing and re-adding the sort description, then?
That kind of defeats the point, and doesn't fit in with the whole data-bound system at all. Binding takes care of updating when an item is added or removed, so I was expecting it to update when the item is changed too. Sadly, it looks like this is not the case, making SortDescription on ListCollectionView pretty much useless for non-static underlying data.
Removing and re-adding the sort description works but is terribly slow...
It seems that the ObservableCollection ignores the OnPropertyChanged events of its children (or at least doesn't pass it to the SortDescription). Maybe theres another event you could fire to get the SortDescription aware of the changes
Regards,
ITD
ListCollectionView does not listen to property change events, and so does not re-sort an item whose sort key changes. There are at least three reasons why we didn't include this feature:
a) Some people don't want it. For example, if you change the key of an item in a ListBox, moving that item to a new position in the ListBox (because it sorts differently) would produce a very "jerky" and confusing user experience.
b) It's expensive. Listening for all those property change events has a non-trivial cost.
c) It's not always possible. For example, ListCollectionView has a CustomSort feature, where the user supplies a custom comparison method. ListCollectionView has no idea how that method compares items, so it has no idea what property changes to listen for.
The same remarks apply to filtering and grouping as well.
This is a common request - to react to property changes dynamically, and re-sort (or re-filter, or re-group). It's on our list of possible features for a future release. It will almost certainly end up being an "opt-in" feature; you'll have to ask for it, and provide a list of properties to listen for (if we can't deduce them from existing information).