■ Storyboard 엘리먼트에서 실행시 동적으로 할당하는 방법을 보여준다.
▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<StackPanel Orientation="Horizontal"> <StackPanel.Resources> <Storyboard x:Name="storyboard1" Completed="storyboard_Completed"> <DoubleAnimation x:Name="doubleAnimation1" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:2" From="1.0" To="0.0" /> </Storyboard> <Storyboard x:Name="storyboard2" Completed="storyboard_Completed"> <DoubleAnimation x:Name="doubleAnimation2" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:2" From="1.0" To="0.0" /> </Storyboard> <Storyboard x:Name="storyboard3" Completed="storyboard_Completed"> <DoubleAnimation x:Name="doubleAnimation3" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:2" From="1.0" To="0.0" /> </Storyboard> </StackPanel.Resources> <Rectangle x:Name="rectangle1" Margin="3" Width="100" Height="100" Fill="Blue" MouseLeftButtonDown="rectangle_MouseLeftButtonDown" /> <Rectangle x:Name="rectangle2" Margin="3" Width="100" Height="100" Fill="Blue" MouseLeftButtonDown="rectangle_MouseLeftButtonDown" /> <Rectangle x:Name="rectangle3" Margin="3" Width="100" Height="100" Fill="Blue" MouseLeftButtonDown="rectangle_MouseLeftButtonDown" /> <Rectangle x:Name="rectangle4" Margin="3" Width="100" Height="100" Fill="Blue" MouseLeftButtonDown="rectangle_MouseLeftButtonDown" /> </StackPanel> |
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
using System; using System.Windows.Input; using System.Windows.Shapes; using System.Windows.Media.Animation; /// <summary> /// 스토리보드 1 활성 여부 /// </summary> bool isStoryboard1Active = false; /// <summary> /// 스토리보드 2 활성 여부 /// </summary> bool isStoryboard2Active = false; /// <summary> /// 스토리보드 3 활성 여부 /// </summary> bool isStoryboard3Active = false; #region 스토리보드 완료시 처리하기 - storyboard_Completed(sender, e) /// <summary> /// 스토리보드 완료시 처리하기 /// </summary> /// <param name="pSender">이벤트 발생자</param> /// <param name="pEventArgs">이벤트 인자</param> private void storyboard_Completed(object sender, EventArgs e) { Storyboard storyboard = sender as Storyboard; switch(storyboard.GetValue(NameProperty).ToString()) { case "storyboard1" : this.isStoryboard1Active = false; break; case "storyboard2" : this.isStoryboard2Active = false; break; case "storyboard3" : this.isStoryboard3Active = false; break; } } #endregion #region 사각형 마우스 왼쪽 버튼 DOWN시 처리하기 - rectangle_MouseLeftButtonDown(sender, e) /// <summary> /// 사각형 마우스 왼쪽 버튼 DOWN시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void rectangle_MouseLeftButtonDown(object sender, MouseEventArgs e) { Rectangle rectangle = pSender as Rectangle; if(!this.isStoryboard1Active) { this.storyboard1.Stop(); this.doubleAnimation1.SetValue(Storyboard.TargetNameProperty, rectangle.Name); this.storyboard1.Begin(); this.isStoryboard1Active = true; } else if(!this.isStoryboard2Active) { this.storyboard2.Stop(); this.doubleAnimation2.SetValue(Storyboard.TargetNameProperty, rectangle.Name); this.storyboard2.Begin(); this.isStoryboard2Active = true; } else if(!this.isStoryboard3Active) { this.storyboard3.Stop(); this.doubleAnimation3.SetValue(Storyboard.TargetNameProperty, rectangle.Name); this.storyboard3.Begin(); this.isStoryboard3Active = true; } } #endregion |