ObservableCollection和INotifyPropertyChanged有什么区别? [英] What is the difference between ObservableCollection and INotifyPropertyChanged?
问题描述
我对ObservableCollection
和INotifyPropertyChanged
的工作方式感到困惑.
I am confused about how ObservableCollection
and INotifyPropertyChanged
works.
我有此代码:
Payments = new ObservableCollection<PaymentViewModel>(_allPayments);
public ObservableCollection<PaymentViewModel> Payments
{
get { return _payments; }
set {
_payments = value;
RaisePropertyChanged("Payments");
}
}
在这里我不了解ObservableCollection
和INotifyPropertyChanged
之间的关系.你能解释一下吗?
I don't understand what is the relationship between ObservableCollection
and INotifyPropertyChanged
here. Can you explain?
推荐答案
ObservableCollection
是一个专用集合,可以在其内容发生更改时通知订阅者,而INotifyPropertyChanged
是一个允许实现者的接口.在其属性之一的值更改时通知订阅者.
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).
考虑以下代码:
var model = new MyViewModel(); // assume it's the class with Payments inside
model.Payments.Add(new PaymentViewModel());
INotifyCollectionChanged.CollectionChanged
事件的订阅者现在将知道情况已经改变,应该相应地进行更新.
Subscribers to the INotifyCollectionChanged.CollectionChanged
event would now know that things have changed and they should update accordingly.
但是现在看看这个:
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>();
将项目添加到集合后,我们将整个集合交换为另一个项目.如果ItemsControl
绑定到此集合,我们希望它会自动更新并反映model.Payments
最终为空的事实.但是它怎么办呢?
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
将无济于事,因为原始集合(在收到第一个项目之后)没有被修改;我们只是丢弃了它,并在其位置安装了另一个.唯一知道切换发生的人是Payments
属性设置器.因此,设置者利用INotifyPropertyChanged
告诉订阅者该集合已被另一个替换,因此他们当然应该更新其状态.
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.
结论:数据绑定在WPF中自动进行,因为如果绑定目标实现了INotifyPropertyChanged
. >他们也同意这一点.如果绑定目标发生更改,则会通过INotifyPropertyChanged
通知它们,请取消订阅旧目标上的INotifyCollectionChanged
并订阅新目标上的.
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.
这篇关于ObservableCollection和INotifyPropertyChanged有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!