c# - apply same storyboard in multiple control -
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:class="storyboard.mainwindow" x:name="window" title="mainwindow" width="640" height="480"> <window.resources> <storyboard x:key="all_in_one"> <coloranimationusingkeyframes storyboard.targetproperty="(panel.background).(gradientbrush.gradientstops)[3].(gradientstop.color)" storyboard.targetname="btn_a"> <easingcolorkeyframe keytime="0" value="#ffcdcdcd"/> <easingcolorkeyframe keytime="0:0:0.6" value="#ffed0b00"/> <easingcolorkeyframe keytime="0:0:1" value="#ffcdcdcd"/> </coloranimationusingkeyframes> </storyboard> </window.resources> <grid x:name="layoutroot"> <button x:name="btn_a" content="a" horizontalalignment="left" height="56" margin="117,134,0,0" verticalalignment="top" width="66" click="btn_a_click" /> <button x:name="btn_b" content="b" horizontalalignment="left" height="56" margin="187,134,0,0" verticalalignment="top" width="66"/> <button x:name="btn_c" content="c" height="56" margin="257,134,301,0" verticalalignment="top"/> </grid> 
here created storyboard button a("btn_a").
public partial class mainwindow : window { public mainwindow() { this.initializecomponent(); // insert code required on object creation below point. } private void btn_a_click(object sender, routedeventargs e) { storyboard button_animation = (storyboard)(findresource("all_in_one")); button_animation.begin(); } } i want apply same storyboard each other buttons such btn_b , btn_c in code behind dynamically.
if click button b,it has animate , button c well.
this not best way this. being said, this:
private void btnc_click(object sender, routedeventargs e) { storyboard storyboard = resources["all_in_one"] storyboard; storyboard.settargetname(storyboard, "btnc"); storyboard.begin(); } you need remove storyboard.targetname assignment xaml work.
if need launch multiple storyboards @ once, call begin() additional times after calling storyboard.settargetname function new target name.
Comments
Post a Comment