views:

736

answers:

3

Hi,

I've seen lots of talk about this question but maybe I'm just too much of a newbie to get it. If I have an observable collection that is a collection of "PersonNames" as in the msdn example (http: //msdn.microsoft.com/en-us/library/ms748365.aspx), I get updates to my View if a PersonName is added or removed, etc. I want to get an update to my View when I change a property in the PersonName as well. Like if I change the first name. I can implement OnPropertyChanged for each property and have this class derive from INotifyPropertyChanged and that seems to get called as expected. My question is, how does the View get the updated data from the ObservableCollection as the property changed does not cause any event for the ObservableCollection. This is probably something really simple but why I can't seem to find an example surprises me. Can anyone shed any light on this for me or have any pointers to examples I would greatly appreciate it. We have this scenario in multiple places in our current WPF app and are struggling with figuring it out.

thanks in advance! Bill

+1  A: 

As you found out, there is no collection-level event that indicates that a property of an item in the collection has changed. Generally, the code responsible for displaying the data adds a PropertyChanged event handler to each object currently displayed onscreen.

binarycoder
Thanks. I am using WPF and have a DataGrid whose ItemsSource is binding in XAML to the ObservableCollection. So, I need to add code somewhere in my ViewModel to handle the PropertyChanged event in order for the View to know to update the DataGrid? And then do I have to remove and add the item to the collection to get it the View to update it? It's seems counter intuitive (but that doesn't mean it's not right :)
Bill Campbell
The DataGrid does this automatically if the elements in the ObservableCollection implement INotifyPropertyChanged (or are DependencyObjects).
Goblin
A: 

"Generally, the code responsible for displaying the data adds a PropertyChanged event handler to each object currently displayed onscreen."

Could someone please give me an example of what this means? My View binds to my ViewModel which has a ObservableCollection. This collection is made up of a RowViewModel which has properties that support the PropertiesChanged event. But I can't figure out how to make the collection update itself so my view will be updated.

thanks! Bill

Bill Campbell
+1  A: 

Here is how you would attach/detach to each item's PropertyChanged event.

    ObservableCollection<INotifyPropertyChanged> items = new ObservableCollection<INotifyPropertyChanged>();
    items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(items_CollectionChanged);


    static void items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        foreach (INotifyPropertyChanged item in e.OldItems)
            item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);

        foreach (INotifyPropertyChanged item in e.NewItems)
            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
    }

    static void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        throw new NotImplementedException();
    }
chilltemp

related questions