I have a listbox bound to a collection. I would like the ListBox to always reverse the order of the items. This handler–hooked up to the control’s load event–works for the initial load, but not thereafter. Ive tried using the SourceUpdated event but that doesnt seem to work.
How do I maintain a constant active sort?
MyList.Items.SortDescriptions.Add(New SortDescription("Content", ListSortDirection.Descending))
How is the collection stored that supplies the items for the ListBox? It should be a collection that supports
INotifyCollectionChanged. The framework providesObservableCollection<T>which you can use.In the constructor of your ViewModel (or wherever the collection lives), you then get the DefaultView for adding the
SortDescription. TheCollectionViewis like a layer on top of your collection, which you can use to sort, group, filter, etc. the items without actually affecting the underlying data source. The framework creates a default one for you. To get a reference to it, you can use code similar to the following:With that in place, you should be able to add items to the
ObservableCollection<T>and the sort order will be maintained.