ObservableCollection PropertyChanged event

后端 未结 4 1006

OK so I want to subclass ObservableCollection to add a property to it. Unfortunately the PropertyChanged event is protected. Basically I want to subcla

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 14:34

    SelectableList intList = new SelectableList();
    ((INotifyPropertyChanged)intList).PropertyChanged += 
        new PropertyChangedEventHandler(intList_Changed);
    

    ObservableCollection implements INotifyPropertyChanged explicitly, which means you have to cast the instance to the interface before you can access the interface's methods, properties and events. As to why this is done, I don't know. The Binding markup extension doesn't "know" ObservableCollections or any other type. It checks types to see if they implement or extend specific interfaces/base classes (INPC, INCC, DependencyObject, etc) and so doesn't care if the interface is implemented explicitly.

提交回复
热议问题