What is the difference between ObservableCollection and INotifyPropertyChanged?

前端 未结 2 1016
抹茶落季
抹茶落季 2021-01-12 13:50

I am confused about how ObservableCollection and INotifyPropertyChanged works.

I have this code:

Payments = new ObservableC         


        
相关标签:
2条回答
  • 2021-01-12 14:31

    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).

    0 讨论(0)
  • 2021-01-12 14:36

    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.

    0 讨论(0)
提交回复
热议问题