c# - INotifyPropertyChanged bubbling in class hierarchy -


suppose, have following class hierarchy:

public nestedclass : inotifypropertychanged {     string prop1;      public string  prop1 {         { 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 {         { 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 class class1 contains properties , several collections of different classes. collections defined in same collection of nestedclass. bound ui, have no problems there.

class1 object , nested objects/collections can created @ runtime, or can generated xml deserializer. handle deserialized collection, have subscribe inotifypropertychanged event of each collection item when collection assigned.

my goal handle changes of items of nestedclass in class1 information data changed (and pass information parent).

the problem have mutiple nested collections , notify parent class class1 , bubbles event parent. however, 1 collection notifies parent class1 changes, handler in class1 null, result class1 doesn't bubble event parent class (is not shown here irrelevant). went through debugger unable find problem. when other nested collections call parent's onpropertychanged, handler not null , working correctly.

edit: issue raised when class1 , nestedclass objects generated xml deserializer.

i read lot of similar posts on so, of them invalid datacontext in view, not case (i believe). else can check find root of issue?

you might want consider switching bindinglist<t> instead of observeablecollection<t>, has logic in childoncollectionchanged built in it. make sure raiselistchangedevents set true , listchanged event raised args.listchangedtype == listchangedtype.itemchanged time 1 of members raises propertychanged event.


Comments