wpf - Using binding in VisualState Storyboard -
i writing custom control wpf application. want use color animation in storyboard
in visualstate
definition. to
property of animation should bound dependency property of control object. not appear work.
i have found thread in silverlight forum describing exact same problem, in claimed works in sl4 rtm: http://forums.silverlight.net/forums/p/174655/423324.aspx. however, when try using code posted in vs2010 wpf application not work, meaning color not change. binding have been able within visualstate
storyboard
staticresource
.
any ideas?
edit:
added code snippets:
from generic.xaml:
<style targettype="{x:type local:testcustomcontrol}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type local:testcustomcontrol}"> <border borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" name="myborder"> <border.background> <solidcolorbrush color="{binding relativesource={relativesource templatedparent}, path=coldcolor}" /> </border.background> <visualstatemanager.visualstategroups> <visualstategroup x:name="commonstates"> <visualstate x:name="normal"/> <visualstate x:name="mouseover"> <storyboard> <!-- works: --> <!--<coloranimation storyboard.targetproperty="background.color" storyboard.targetname="myborder" to="red" duration="0:0:0.2"/>--> <!-- works: --> <!--<coloranimation storyboard.targetproperty="background.color" storyboard.targetname="myborder" to="{staticresource hotcolorres}" duration="0:0:0.2"/>--> <!-- doesn't work: --> <coloranimation storyboard.targetproperty="background.color" storyboard.targetname="myborder" to="{binding relativesource={relativesource templatedparent}, path=hotcolor}" duration="0:0:0.2"/> </storyboard> </visualstate> </visualstategroup> </visualstatemanager.visualstategroups> </border> </controltemplate> </setter.value> </setter> </style>
testcustomcontrol.cs:
public class testcustomcontrol : button { static testcustomcontrol() { defaultstylekeyproperty.overridemetadata(typeof(testcustomcontrol), new frameworkpropertymetadata(typeof(testcustomcontrol))); } public color hotcolor { { return (color)getvalue(hotcolorproperty); } set { setvalue(hotcolorproperty, value); } } // using dependencyproperty backing store hotcolor. enables animation, styling, binding, etc... public static readonly dependencyproperty hotcolorproperty = dependencyproperty.register("hotcolor", typeof(color), typeof(testcustomcontrol), new uipropertymetadata(colors.aqua)); public color coldcolor { { return (color)getvalue(coldcolorproperty); } set { setvalue(coldcolorproperty, value); } } // using dependencyproperty backing store coldcolor. enables animation, styling, binding, etc... public static readonly dependencyproperty coldcolorproperty = dependencyproperty.register("coldcolor", typeof(color), typeof(testcustomcontrol), new uipropertymetadata(colors.aqua)); }
it work expected if specify x:name
attribute <coloranimation>
in generic.xaml this:
<!-- work: --> <coloranimation x:name="part_coloranimation" storyboard.targetproperty="background.color" storyboard.targetname="myborder" duration="0:0:0.2" />
and set binding to
property later in code behind in time when template applied control overriding onapplytemplate() in testcustomcontrol.cs:
public override void onapplytemplate() { var coloranimation = (coloranimation)template.findname("part_coloranimation", this); if(coloranimation == null) return; var binding = new binding("hotcolor") { source = }; bindingoperations.setbinding(coloranimation, coloranimation.toproperty, binding); }
hope help.
Comments
Post a Comment