ObservableCollection如何工作?

时间:2010-08-26 16:38:32

标签: binding observablecollection

我正在查看ObservableCollection代码(感谢令人敬畏的.NET Reflector)并且惊讶地发现Add和Remove方法没有被覆盖。然后ObservableCollection如何引发PropertyChanged或CollectionChanged事件以在添加或删除某些内容时通知?

1 个答案:

答案 0 :(得分:2)

它覆盖了基本Collection< T>的一系列受保护方法。等级,例如InsertItem(int index,T item),RemoveItem(int index)等

这些覆盖专门引发事件:

protected override void InsertItem(int index, T item)
{
    this.CheckReentrancy();
    base.InsertItem(index, item);
    this.OnPropertyChanged("Count");
    this.OnPropertyChanged("Item[]");
    this.OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
}