c# - wpf INotifyCollectionChanged redundancy -


i've written custom control , have 2 collection dependency properties sourcea , sourceb. you'll see in code below dependency properties 'propertychanged' methods same, seems lot of redundant code.

sourceb_propertychanged... sourcea_propertychanged... 

i don't feel i'm doing properly. there way me remove redundancy while maintaining notification collection changed?

the code

using system.collections; using system.collections.objectmodel; using system.collections.specialized; using system.windows; using system.windows.controls; using system.windows.media;  namespace nexuseditor {     /// <summary>     /// main class implements network/flow-chart control.     /// </summary>     public partial class nexuseditor : control     {         #region dependency property/event definitions          private static readonly dependencypropertykey itemspropertykey =             dependencyproperty.registerreadonly("items", typeof(observablecollection<object>), typeof(nexuseditor),                 new frameworkpropertymetadata());         public static readonly dependencyproperty itemsproperty = itemspropertykey.dependencyproperty;          public static readonly dependencyproperty sourceaproperty =             dependencyproperty.register("sourcea", typeof(ienumerable), typeof(nexuseditor),                 new frameworkpropertymetadata(sourcea_propertychanged));          public static readonly dependencyproperty sourcebproperty =             dependencyproperty.register("sourceb", typeof(ienumerable), typeof(nexuseditor),                 new frameworkpropertymetadata(sourceb_propertychanged));          #endregion           #region constructors          public nexuseditor()         {             // create collection contain nodes.             this.items = new observablecollection<object>();         }          static nexuseditor()         {             defaultstylekeyproperty.overridemetadata(typeof(nexuseditor), new frameworkpropertymetadata(typeof(nexuseditor)));         }          #endregion          #region properties          public ienumerable sourcea         {             { return (ienumerable)getvalue(sourceaproperty); }             set { setvalue(sourceaproperty, value); }         }          public ienumerable sourceb         {             { return (ienumerable)getvalue(sourcebproperty); }             set { setvalue(sourcebproperty, value); }         }          /// <summary>         /// collection of items         /// </summary>         public observablecollection<object> items         {             { return (observablecollection<object>)getvalue(itemsproperty); }             private set { setvalue(itemspropertykey, value); }         }          #endregion          #region privates          private static void updateitems(nexuseditor editor)         {             editor.items.clear();              var sourceb = editor.sourceb ienumerable;             if (sourceb != null)             {                 foreach (object obj in sourceb)                 {                     editor.items.add(obj);                 }             }              var sourcea = editor.sourcea ienumerable;             if (sourcea != null)             {                 foreach (object obj in sourcea)                 {                     editor.items.add(obj);                 }             }         }          /// <summary>         /// event raised when new collection has been assigned sourceb property.         /// </summary>         private static void sourceb_propertychanged(dependencyobject d, dependencypropertychangedeventargs e)         {             nexuseditor c = (nexuseditor)d;              if (e.oldvalue != null)             {                 var notifycollectionchanged = e.oldvalue inotifycollectionchanged;                 if (notifycollectionchanged != null)                 {                     notifycollectionchanged.collectionchanged -= new notifycollectionchangedeventhandler(c.sourceb_collectionchanged);                 }             }              if (e.newvalue != null)             {                 var notifycollectionchanged = e.newvalue inotifycollectionchanged;                 if (notifycollectionchanged != null)                 {                     notifycollectionchanged.collectionchanged += new notifycollectionchangedeventhandler(c.sourceb_collectionchanged);                 }             }              updateitems(c);         }          /// <summary>         /// event raised when node has been added or removed collection assigned 'nodessource'.         /// </summary>         private void sourceb_collectionchanged(object sender, notifycollectionchangedeventargs e)         {             updateitems(this);         }           /// <summary>         /// event raised when new collection has been assigned sourceb property.         /// </summary>         private static void sourcea_propertychanged(dependencyobject d, dependencypropertychangedeventargs e)         {             nexuseditor c = (nexuseditor)d;              if (e.oldvalue != null)             {                 var notifycollectionchanged = e.oldvalue inotifycollectionchanged;                 if (notifycollectionchanged != null)                 {                     notifycollectionchanged.collectionchanged -= new notifycollectionchangedeventhandler(c.sourcea_collectionchanged);                 }             }              if (e.newvalue != null)             {                 var notifycollectionchanged = e.newvalue inotifycollectionchanged;                 if (notifycollectionchanged != null)                 {                     notifycollectionchanged.collectionchanged += new notifycollectionchangedeventhandler(c.sourcea_collectionchanged);                 }             }              updateitems(c);         }          /// <summary>         /// event raised when node has been added or removed collection assigned 'nodessource'.         /// </summary>         private void sourcea_collectionchanged(object sender, notifycollectionchangedeventargs e)         {             updateitems(this);         }          #endregion          public override void onapplytemplate()         {             base.onapplytemplate();          }     } } 

why don't create 1 propertychanged handler method named source_propertychanged example , use both dependency properties?

if they're same can that.

public static readonly dependencyproperty sourceaproperty =     dependencyproperty.register("sourcea", typeof(ienumerable), typeof(nexuseditor),      new frameworkpropertymetadata(source_propertychanged));  public static readonly dependencyproperty sourcebproperty =     dependencyproperty.register("sourceb", typeof(ienumerable), typeof(nexuseditor),     new frameworkpropertymetadata(source_propertychanged)); 

the handler:

private static void source_propertychanged(dependencyobject d, dependencypropertychangedeventargs e) {     nexuseditor c = (nexuseditor)d;      if (e.oldvalue != null)     {         var notifycollectionchanged = e.oldvalue inotifycollectionchanged;         if (notifycollectionchanged != null)         {             notifycollectionchanged.collectionchanged -= new notifycollectionchangedeventhandler(c.source_collectionchanged);         }     }      if (e.newvalue != null)     {         var notifycollectionchanged = e.newvalue inotifycollectionchanged;         if (notifycollectionchanged != null)         {             notifycollectionchanged.collectionchanged += new notifycollectionchangedeventhandler(c.source_collectionchanged);         }     }      updateitems(c); } 

for collection changed:

private void source_collectionchanged(object sender, notifycollectionchangedeventargs e) {     updateitems(this); } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -