c# - Fetch the template of a DependyObject received in an Attached Property's callback -
c# - Fetch the template of a DependyObject received in an Attached Property's callback -
i'm developing application in wpf (.net 4).
i have attached property created in order determine if command in "valid" state.
/// <summary> /// initializes static members of <see cref="validationproperty"/> class. /// </summary> static validationproperty() { // register attached dependency property. isvalidproperty = dependencyproperty.registerattached("isvalid", typeof(bool), typeof(validationproperty), new frameworkpropertymetadata(true, validationvaluechanged)); } (...) /// <summary> /// gets or sets dependency property used determine if element valid. /// </summary> public static dependencyproperty isvalidproperty { get; set; } (...)
i play animation whenever value false. animation either defined within command itself, or in template. name of storyboard constant, i.e.: "invalidinput".
here callback:
/// <summary> /// event raised when "is valid" dependency property's value changes. /// </summary> /// <param name="sender">the object raised event.</param> /// <param name="e">the arguments passed event.</param> private static void validationvaluechanged(dependencyobject sender, dependencypropertychangedeventargs e) { var invalidcontrol = sender usercontrol; if (invalidcontrol == null || !(e.newvalue bool)) { return; } var isvalid = (bool)e.newvalue; if (isvalid) { return; } var usercontrolinvalidinputstoryboard = invalidcontrol.resources["invalidinput"] storyboard; var templatedinvalidinputstoryboard = invalidcontrol.template.resources["invalidinput"] storyboard; if (usercontrolinvalidinputstoryboard != null) { usercontrolinvalidinputstoryboard.begin(invalidcontrol); return; } if (templatedinvalidinputstoryboard != null) { templatedinvalidinputstoryboard.begin(invalidcontrol, invalidcontrol.template); return; } }
the problem right i'm unable find storyboard defined within either command or template. both usercontrolinvalidinputstoryboard , templatedinvalidinputstoryboard null.
i suspect due fact dependency object cast user command instead of real type.
is there way retrieve template , storyboard ?
edit: requested, here xaml of command uses attached property
<usercontrol (...) xmlns:attachedproperties="clr-namespace:controls.attachedproperties" x:name="root"> <grid x:name="layoutroot"> <textbox x:name="textbox" text="{binding path=(attachedproperties:textproperty.text), updatesourcetrigger=propertychanged, relativesource={relativesource ancestortype={x:type custom:usernametextbox}}, mode=twoway, targetnullvalue={x:static custom:usernametextbox.defaultkeyword}, fallbackvalue=defaultkeyword}" horizontalalignment="left" width="292" style="{dynamicresource standardtextboxstyle}" height="35" verticalalignment="top" tabindex="0" maxlines="1" maxlength="30" fontsize="16" gotfocus="textboxgotfocus" lostfocus="textboxlostfocus" textchanged="textboxtextchanged" fontweight="bold"/> </grid>
and here's how instantiated control:
<custom:usernametextbox x:name="usernametextbox" attachedproperties:validationproperty.isvalid="{binding codexloginservice.usernameisvalid, updatesourcetrigger=propertychanged}" attachedproperties:textproperty.text="{binding codexloginservice.username, mode=twoway, updatesourcetrigger=propertychanged}" keyup="onusernamechanged"/>
edit #2: proposed, created visual state grouping in template:
<visualstategroup x:name="invalidstates"> <visualstategroup.transitions> <visualtransition generatedduration="0:0:0.2" to="invalid"/> </visualstategroup.transitions> <visualstate x:name="invalid"> <storyboard> <doubleanimationusingkeyframes storyboard.targetproperty="(uielement.opacity)" storyboard.targetname="invalidframe"> <easingdoublekeyframe keytime="0" value="1"/> </doubleanimationusingkeyframes> </storyboard> </visualstate> </visualstategroup>
attempting alter state of command not work, method returns false:
visualstatemanager.gotostate(invalidcontrol, "invalid", true);
if implement visualstates can forget name of animation , alter state. check this article.
c# .net wpf
Comments
Post a Comment