c# - How to create BeginStoryboard in code behind for WPF? -
i have following xaml , wish convert code behind, have been able create animations control fades in & out expected, i'm having trouble converting ismouseover trigger code behind:
<datatemplate.triggers> <eventtrigger routedevent="control.loaded" sourcename="notificationgrid"> <beginstoryboard x:name="beginnotificationstoryboard"> <storyboard x:name="notificationstoryboard"> <doubleanimation storyboard.targetname="notificationgrid" from="0.01" to="1" storyboard.targetproperty="opacity" duration="0:0:0.5" /> <doubleanimation storyboard.targetname="notificationgrid" from="1" to="0" storyboard.targetproperty="opacity" duration="0:0:0.5" begintime="0:0:5" /> </storyboard> </beginstoryboard> </eventtrigger> <trigger property="ismouseover" value="true"> <trigger.enteractions> <seekstoryboard offset="0:0:3" beginstoryboardname="beginnotificationstoryboard" /> <pausestoryboard beginstoryboardname="beginnotificationstoryboard" /> </trigger.enteractions> <trigger.exitactions> <seekstoryboard offset="0:0:3" beginstoryboardname="beginnotificationstoryboard" /> <resumestoryboard beginstoryboardname="beginnotificationstoryboard" /> </trigger.exitactions> </trigger> </datatemplate.triggers> </datatemplate>
the issue have how know 'beginstoryboardname' value should instances of seekstoryboard classes since haven't had create beginstoryboard instance animation on loading work expected.
var loadinganimation = new doubleanimation(0.01, 1, new duration(timespan.fromseconds(0.5))); var closinganimation = new doubleanimation(1, 0, new duration(timespan.fromseconds(3))) { begintime = timespan.fromseconds(5) }; storyboard.settarget(loadinganimation, associatedobject); storyboard.settarget(closinganimation, associatedobject); storyboard.settargetproperty(loadinganimation, new propertypath(uielement.opacityproperty)); storyboard.settargetproperty(closinganimation, new propertypath(uielement.opacityproperty)); storyboard.settarget(loadinganimation, associatedobject); storyboard.settarget(closinganimation, associatedobject); var storyboard = new storyboard(); storyboard.children.add(loadinganimation); storyboard.children.add(closinganimation); var enterseekstoryboard = new seekstoryboard { offset = timespan.fromseconds(5), // value should go here? beginstoryboardname = "" }; var exitseekstoryboard = new seekstoryboard { offset = timespan.fromseconds(5), // value should go here? beginstoryboardname = "" }; var trigger = new trigger { property = uielement.ismouseoverproperty, value = true }; trigger.enteractions.add(enterseekstoryboard); trigger.exitactions.add(exitseekstoryboard); var style = new style(); style.triggers.add(trigger); associatedobject.style = style; storyboard.completed += handleoncompleted; storyboard.begin();
your full code this. i've left comments point out issues i've found:
var loadinganimation = new doubleanimation(0.01, 1, new duration(timespan.fromseconds(0.5))); var closinganimation = new doubleanimation(1, 0, new duration(timespan.fromseconds(3))) { begintime = timespan.fromseconds(5) }; storyboard.settarget(loadinganimation, associatedobject); storyboard.settarget(closinganimation, associatedobject); storyboard.settargetproperty(loadinganimation, new propertypath(uielement.opacityproperty)); storyboard.settargetproperty(closinganimation, new propertypath(uielement.opacityproperty)); storyboard.settarget(loadinganimation, associatedobject); storyboard.settarget(closinganimation, associatedobject); var storyboard = new storyboard(); storyboard.children.add(loadinganimation); storyboard.children.add(closinganimation); // subscription events must done @ point, because storyboard object becomes frozen later on storyboard.completed += handleoncompleted; string storyboardname = "beginnotificationstoryboard"; // define beginstoryboard action eventtrigger var beginstoryboard = new beginstoryboard(); beginstoryboard.name = storyboardname; beginstoryboard.storyboard = storyboard; // create eventtrigger var eventtrigger = new eventtrigger(control.loadedevent); eventtrigger.actions.add(beginstoryboard); // actions entering animation var enterseekstoryboard = new seekstoryboard { offset = timespan.fromseconds(5), beginstoryboardname = storyboardname }; var enterpausestoryboard = new pausestoryboard { beginstoryboardname = storyboardname }; // actions exiting animation var exitseekstoryboard = new seekstoryboard { offset = timespan.fromseconds(5), beginstoryboardname = storyboardname }; var exitresumestoryboard = new resumestoryboard { beginstoryboardname = storyboardname }; var trigger = new trigger { property = uielement.ismouseoverproperty, value = true }; trigger.enteractions.add(enterseekstoryboard); trigger.enteractions.add(enterpausestoryboard); trigger.exitactions.add(exitseekstoryboard); trigger.exitactions.add(exitresumestoryboard); var style = new style(); // name of storyboard must registered actions can find style.registername(storyboardname, beginstoryboard); // add both eventtrigger , regular trigger style.triggers.add(eventtrigger); style.triggers.add(trigger); associatedobject.style = style; // no need storyboard.begin()
Comments
Post a Comment