Refreshing the order of the Listbox when item's details has changed
How can I re-sort my Listbox when an item's name is changed using MVVM?
My Listbox is bound to an Observable collection that stores a 'People' class that inherits INotifyPropertyChanged. The Listbox is sorted on property 'Name' and when I change the value of 'Name' for one of the items in the Listbox, I can see that it's value has changed but the Listbox does not automatically re-sort itself.
The initial sorting is done through a CollectionViewSource
<CollectionViewSource x:Key="SortedItems" Source="{Binding PeopleList}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Name"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
and is used by the Listbox as follows
<ListBox ItemsSource="{Binding Source={StaticResource SortedItems}}"
SelectedItem="{Binding CurrentSelectedPerson}">开发者_JAVA技巧;
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When I go to a different page and come back the Listbox has the correct ordering, but when I change the Name of the person the Listbox doesn't refresh. How can this be done while preserving MVVM?
Well this is a known problem which i guess is not been addressed yet.Try implementing a custom CollectionViewSource as mentioned in the below link
http://social.msdn.microsoft.com/Forums/en/wpf/thread/d7eda358-ca16-4164-8773-fd92527c7795
also check this thread for an alternative
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cb7c5c62-7ca9-49b5-91a0-379581b1c1aa/
精彩评论