Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ObservableCollection and INotifyPropertyChanged?

Tags:

mvvm

wpf

I am confused about how ObservableCollection and INotifyPropertyChanged works.

I have this code:

Payments = new ObservableCollection<PaymentViewModel>(_allPayments);

public ObservableCollection<PaymentViewModel> Payments
{
    get { return _payments; }
    set {
        _payments = value;
        RaisePropertyChanged("Payments");
    }
}

I don't understand what is the relationship between ObservableCollection and INotifyPropertyChanged here. Can you explain?

like image 376
pratikjgandhi Avatar asked Apr 20 '12 13:04

pratikjgandhi


2 Answers

ObservableCollection is a specialized collection that can notify subscribers when its contents change, while INotifyPropertyChanged is an interface that allows implementors to notify subscribers when one of their properties changes value.

You are probably wondering how the two are related (because both are "involved" in the setter in your example).

Consider this code:

var model = new MyViewModel(); // assume it's the class with Payments inside
model.Payments.Add(new PaymentViewModel());

Subscribers to the INotifyCollectionChanged.CollectionChanged event would now know that things have changed and they should update accordingly.

But now look at this:

var model = new MyViewModel(); // assume it's the class with Payments inside
model.Payments.Add(new PaymentViewModel()); // OK, we know what this does

model.Payments = new ObservableCollection<PaymentViewModel>();

After adding an item to the collection we then swap the entire collection for another one. If an ItemsControl is bound to this collection we expect it to update itself and reflect the fact that model.Payments ends up being empty. But how can it do that?

CollectionChanged will not help because the original collection (after receiving its first item) was not modified; we just discarded it and installed another in its place. The only one who knows that the switch happened is the Payments property setter. So the setter utilizes INotifyPropertyChanged to tell subscribers that the collection has been replaced with another, and they should of course update their status.

Conclusion: Data binding works automagically in WPF because all the databound controls listen to the INotifyPropertyChanged of their DataContext, and if the binding target implements INotifyCollectionChanged they subscribe to that as well. If the binding target changes they are notified through INotifyPropertyChanged, unsubscribe from INotifyCollectionChanged on the old target and subscribe to it on the new one.

like image 58
Jon Avatar answered Sep 21 '22 12:09

Jon


An ObservableCollection notifies its binder, with for example when something is added/deleted to the collection itself.

What you're doing in the second example however, is notifying the binder that the collection has been replaced by a new collection (something that the ObservableCollection does not do itself, if you do new ObservableCollection - there you lose the binding).

like image 37
Amadeus Hein Avatar answered Sep 21 '22 12:09

Amadeus Hein