It would be best if gave your list items (models) some additional functionality that is intended to simply service the view, viewmodel-style -- and of course you can wrap your models inside viewmodels if you want to keep them pure.
For example, you can give your models a Changed
property that you can set (or, the Value
property setter can change it automatically whenever the value changes). This property can then be used to sort the items in the ListBox
by sorting the collection view that the box is bound to:
<CollectionViewSource x:Key="SortedModels" Source="{Binding ListOfModels}">
<CollectionViewSource.SortDescriptions>
<SortDescription PropertyName="Changed"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<ListBox ItemsSource="{Binding Source={StaticResource SortedModels}}"/>
Assuming that your models implement INotifyPropertyChanged
and ListOfModels
is an ObservableCollection
, that's all it will take to have sorting done automatically.