c# - One binding to user control works, second doesn't -
i've created user control, code property want bind is:
public color value { { return (color)this.getvalue(this.valueproperty); } set { this.setvalue(this.valueproperty, value); } } public readonly dependencyproperty valueproperty = dependencyproperty.register("value", typeof(color), typeof(colorslider), new propertymetadata(colors.red))
i have 2 instances of control in page:
<local:colorslider x:name="colorsslider1" /> <!--...--> <local:colorslider x:name="colorsslider3" />
and controls values, want bind (from colorslider
canvas
, textblock
):
<canvas x:name="tilecanvas" grid.column="0" margin="30" width="173" height="173" background="{binding value, elementname=colorsslider1, converter={staticresource colortosolidbrushconverter}}"> <textblock x:name="tiletext" text="dsdfsdfsf" foreground="{binding value, elementname=colorsslider3, converter={staticresource colortosolidbrushconverter}}"/> </canvas>
so here's problem. binding canvas
works, binding textblock
doesn't! interesting if remove colorslider3
binding textblock
work! binding update textblock
background if set binding colorslider3
.
so seems can bind values latest instance of 1 usercontrol
. why case , how can fix it?
your dependency property not correctly defined... lacks static part, , don't need "this":
public color value { { return (color)this.getvalue(valueproperty); } set { this.setvalue(valueproperty, value); } } public static readonly dependencyproperty valueproperty = dependencyproperty.register("value", typeof(color), typeof(colorslider), new propertymetadata(colors.red));
edit -----------------
good know works you. however, real reason ms decided static... never though it, according msdn:
"custom dependency properties.
if want properties on custom types support value expressions, property invalidation, per-type default values, inheritance, data binding, animation, or styling, should these clr properties dependency property following these guidelines , procedures:
- register dependency property using register method; method returns dependencyproperty, should store accessible static read-only field in class..."
Comments
Post a Comment