问题
Suppose, I have the following class hierarchy:
public NestedClass : INotifyPropertyChanged {
string prop1;
public String Prop1 {
get { return prop1; }
set {
prop1 = value;
OnPropertyChanged("Prop1");
}
}
void OnPropertyChanged(String name) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public Class1 : INotifyPropertyChanged {
ObservableCollection<NestedClass> collection;
public Class1() {
collection = new ObservableCollection<NestedClass>();
}
public ObservableCollection<NestedClass> Collection {
get { return collection; }
set {
if (collection != null) {
collection.CollectionChanged -= childOnCollectionChanged;
}
collection = value;
if (collection != null) {
collection.CollectionChanged += childOnCollectionChanged;
}
}
}
void childOnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) {
switch (e.Action) {
case NotifyCollectionChangedAction.Add:
((INotifyPropertyChanged)e.NewItems[0]).PropertyChanged += childOnPropertyChanged;
break;
case NotifyCollectionChangedAction.Remove:
((INotifyPropertyChanged)e.OldItems[0]).PropertyChanged -= childOnPropertyChanged;
break;
case NotifyCollectionChangedAction.Reset:
if (e.OldItems == null) { break; }
foreach (INotifyPropertyChanged itemToRemove in e.OldItems) {
itemToRemove.PropertyChanged -= childOnPropertyChanged;
}
break;
}
}
void childOnPropertyChanged(Object sender, PropertyChangedEventArgs e) {
OnPropertyChanged("nested");
}
void OnPropertyChanged(String name) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
there is class Class1
which contains some properties and several collections of different classes. All collections are defined in the same was as a collection of NestedClass
. They are properly bound to the UI, I have no problems there.
Class1
object and nested objects/collections can be created at runtime, or can be generated by a XML deserializer. To properly handle deserialized collection, I have to subscribe to INotifyPropertyChanged
event of each collection item when collection is assigned.
My goal is to handle all changes of all items of NestedClass
in Class1
to get information that data was changed (and pass this information up to its parent).
The problem is that I have mutiple nested collections and they notify parent class Class1
and it successfully bubbles the event to its parent. However, one collection notifies parent Class1
about changes, but handler
in the Class1
is null, as the result Class1
doesn't bubble event to its parent class (is not shown here as irrelevant). I went through debugger but was unable to find where is the problem. When other nested collections call parent's OnPropertyChanged
, the handler
is not null and everything is working correctly.
Edit: this issue is raised only when Class1
and NestedClass
objects are generated by XML deserializer.
I read a lot of similar posts on SO, but most of them are about invalid DataContext in the view, which is not my case (I believe). What else I can check to find the root of the issue?
回答1:
You might want to consider switching to a BindingList<T> instead of a ObserveableCollection<T>
, it has the logic you do in childOnCollectionChanged
already built in to it. Just make sure RaiseListChangedEvents
is set to true and the ListChanged
event will be raised with args.ListChangedType == ListChangedType.ItemChanged
any time one of the members raises its PropertyChanged
event.
来源:https://stackoverflow.com/questions/38509975/inotifypropertychanged-bubbling-in-class-hierarchy