c# - Using Dependency Properties With Multiple Instances -


i have same problem guy here: error: " 'subjects' property registered 'period' " raised when more 1 control placed on form

the main difference between want subscribe event has access local instance when xaml changes property.

so have usercontrol:

public partial class boardsquare : usercontrol {     public boardsquare()     {         initializecomponent();         location = new boardlocation(int32.minvalue, int32.minvalue);          xpositionproperty =              dependencyproperty.register("xposition", typeof(int),              typeof(boardsquare), new propertymetadata(                  new propertychangedcallback((value, args) =>                   {                       location.x = (int)args.newvalue;                      resetbackgroundcolortoposition();                  })));         ypositionproperty=             dependencyproperty.register("yposition", typeof(int),             typeof(boardsquare), new propertymetadata(                 new propertychangedcallback((value, args)=>                 {                     location.y = (int)args.newvalue;                     resetbackgroundcolortoposition();                 })));     }      private void resetbackgroundcolortoposition()     {         this.background = (brush)(new colorenumtobrushesconverter()).convert(location.getsquarecolor(), typeof(blackwhitecolor), null, null);     }      public readonly dependencyproperty xpositionproperty;     public readonly dependencyproperty ypositionproperty;      public int xposition     {                 {             return (int)getvalue(xpositionproperty);         }         set         {             setvalue(xpositionproperty, value);         }     }      public int yposition      {                  {             return (int)getvalue(ypositionproperty);         }         set         {             setvalue(ypositionproperty, value);         }     }      public boardlocation location { get; set; } } 

here xaml:

<local:boardsquare grid.column="3" grid.row="0" xposition="3" yposition="0"/> <local:boardsquare grid.column="4" grid.row="0" xposition="4" yposition="0"/> 

from understand, solution make xpositionproperty static , register in static constructor. problem can't access local instance of class when propertychangecallback event happens.

how can set property in xaml , still on property changed event in c# code?

is there better solution dependency properties?


below working code of boardsquare after implemented answer.

    public partial class boardsquare : usercontrol {     static boardsquare()     {         xpositionproperty =              dependencyproperty.register("xposition", typeof(int),              typeof(boardsquare), new propertymetadata(                  new propertychangedcallback((objectinstance, args) =>                  {                      boardsquare boardsquare = (boardsquare)objectinstance;                      boardsquare.location.x = (int)args.newvalue;                      boardsquare.resetbackgroundcolortoposition();                  })));         ypositionproperty =             dependencyproperty.register("yposition", typeof(int),             typeof(boardsquare), new propertymetadata(                 new propertychangedcallback((objectinstance, args) =>                 {                     boardsquare boardsquare = (boardsquare)objectinstance;                     boardsquare.location.y = (int)args.newvalue;                     boardsquare.resetbackgroundcolortoposition();                 })));     }      public boardsquare()     {         initializecomponent();         location = new boardlocation(int32.minvalue, int32.minvalue);       }      private void resetbackgroundcolortoposition()     {         this.background = (brush)(new colorenumtobrushesconverter()).convert(location.getsquarecolor(), typeof(blackwhitecolor), null, null);     }      public static readonly dependencyproperty xpositionproperty;     public static readonly dependencyproperty ypositionproperty;      public int xposition     {                 {             return (int)getvalue(xpositionproperty);         }         set         {             setvalue(xpositionproperty, value);         }     }      public int yposition      {                  {             return (int)getvalue(ypositionproperty);         }         set         {             setvalue(ypositionproperty, value);         }     }      public boardlocation location { get; set; } } 

the first argument of propertychangedcallback is local instance (btw. better name obj value avoid confusion). have cast dependencyobject boardsquare , that's all.

public static readonly dependencyproperty xpositionproperty =     dependencyproperty.register("xposition", typeof(int), typeof(boardsquare),                                 new propertymetadata(new propertychangedcallback((obj, args) => {                                     boardsquare bs = obj boardsquare;                                     bs.location.x = (int)args.newvalue;                                     bs.resetbackgroundcolortoposition();                                 }))); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -