Refresh data in ListView
I have a ListView with 3 columns (User Name, Active, and Group) The ItemsSource is bound to a staticresource
ItemsSource="{Binding Source={StaticResource SortedUsers},开发者_如何转开发 UpdateSourceTrigger=PropertyChanged}"
the static resource is defined as:
<CollectionViewSource x:Key="SortedUsers" Source="{Binding UsersList, UpdateSourceTrigger=PropertyChanged}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription
Direction="Ascending"
PropertyName="UserName" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
The user can select an item in the list and click "Modify" to change information about the user or can click an "Add" button to add a new user to the list. Both the Add and Modify use the same modal dialog to allow setup/modify of a new user. Adding a user works fine, the new user shows in the list once the modal dialog closes. however, modifying an existing user doesn't update the data in the ListView until the window that contains the listview is closed then reopened. What do i need to do to have the ListView update for modify?
Did u try using
((CollectionViewSource)this.FindResource("SortedUsers")).View.Refresh();
Although in MVVM the steps above wont be allowed. So if the source collection (UsersList) is ObservableCollection collection view source will atomatically refresh on collection changed notifications of UsersList.
Let me know if this helps.
Do your data objects implement INotifyPropertyChanged interface?
See: https://stackoverflow.com/questions/6713288/databinding-fail-after-using-controltemplate/6713334#6713334
INotifyPropertyChanged notifies the UI that something has changed on the class implementing it. Of note: I dont know what kind of collection you are using to hold your classes (which gets passed into the CollectionViewSource), but I almost always use ObservableCollections. They implement CollectionChanged and notify the UI when items are added, removed and the collection is refreshed.
精彩评论