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