Refresh ObservableCollection in WPF

ObservableCollection only provided notifications when we make any change in the collection itself like Adding or Removing an Item.

If we update any properties of a contained class, it doesn’t raise any notifications.

For e.g.

If I have an ObservableCollection of Person object and it is bind to a TreeView. If I add or remove person object from the ObservableCollection I will be able to see the changes in my TreeView also.

Here Person object has only one string property FullName

Now if I click on Change Name button, which simply renames the selected Person, I will not be able to see the change in my TreeView.

Here in this case we need to call the Refresh method of CollectionViewSource.


CollectionViewSource.GetDefaultView(this.observableCollPerson).Refresh();

https://skydrive.live.com/redir.aspx?cid=2312e1103cbe5ecd&resid=2312E1103CBE5ECD!339&parid=root

Have a look at this solution as well

http://blog.falafel.com/blogs/11-01-30/Using_CollectionViewSource_without_Refresh_for_Faster_Filtering_in_Silverlight

Hope it helps.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

9 thoughts on “Refresh ObservableCollection in WPF”

  1. I extended the ObservableCollection type

    on your ViewModel you can call;

    MyObservableCollection.SendUIRefreshNotification() – To Update all entries in the collection or;

    MyObservableCollection.SendUIRefreshNotification(item) – To only refresh a specific entry

    public static class FwExtensions
    {
    public static void SendUIRefreshNotification(this ObservableCollection observableCollection, T item)
    {
    var index = observableCollection.IndexOf(item);
    observableCollection.Remove(item);
    observableCollection.Insert(index, item);
    }

    public static void SendUIRefreshNotification(this ObservableCollection observablecollection)
    {
    var a = observablecollection.ToList();
    foreach (var item in a)
    {
    observablecollection.SendUIRefreshNotification(item);
    }
    }
    }

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s