event handling - C#, simplified code to handle both changes and updates of a dependency property -


obviously, i'm not expert in c#. simplify code using anonymous handler, or maybe lambda, not sure. valuehaschanged propertychangedcallback used when dp changed, ensures new object monitored update, both changes , updates processed using same code: processnewvalue. pity here create second handler valuehasbeenupdated call same method. there possibility remove definition of valuehasbeenupdated? thanks.

private static void valuehaschanged(     dependencyobject sender, dependencypropertychangedeventargs args) {      // instance     myclass1 instance = sender myclass1;      // unregister on old object     if (args.oldvalue != null) (args.oldvalue myclass2).propertychanged -=         instance.valuehasbeenupdated;     // register updates on new object     if (args.newvalue != null) (args.newvalue myclass2).propertychanged +=         instance.valuehasbeenupdated;      // process new value anyway     instance.processnewvalue(); }  // value has been updated private void valuehasbeenupdated(object sender, propertychangedeventargs e) {      // call actual method process it, not elegant...     processnewvalue(); }  // process new or updated object private void processnewvalue() {...} 

nothing wrong doing you've done. may seem "inelegant," readable. readability more important elegance. there may other solutions, more confusing other coders understand (or understand 6 months now).

stick you've got.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -