■ Timeline 엘리먼트의 CurrentStateInvalidated 이벤트를 사용해 현재 상태 무효시 처리하는 방법을 보여준다.
▶ MainWindow.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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel Orientation="Horizontal"> <ListBox Name="listBox1" Width="200" Height="200" BorderThickness="1" BorderBrush="Black" /> <ListBox Name="listBox2" Margin="10 0 0 0" Width="200" Height="200" BorderThickness="1" BorderBrush="Black" /> <ListBox Name="listBox3" Margin="10 0 0 0" Width="200" Height="200" BorderThickness="1" BorderBrush="Black" /> </StackPanel> <Rectangle Name="rectangle1" Margin="0 10 0 0" Width="100" Height="50" Fill="Orange" /> <Rectangle Name="rectangle2" Margin="0 10 0 0" Width="100" Height="50" Fill="Gray" /> <Button Margin="0 10 0 0" Width="150" Height="30" Content="애니메이션 시작"> <Button.Triggers> <EventTrigger RoutedEvent="Button.Click"> <BeginStoryboard> <Storyboard x:Name="storyboard" RepeatBehavior="2x" AutoReverse="True"> <DoubleAnimation x:Name="widthAnimation1" Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="Width" Duration="00:00:09" BeginTime="00:00:01" From="10" To="200" /> <DoubleAnimation x:Name="widthAnimation2" Storyboard.TargetName="rectangle2" Storyboard.TargetProperty="Width" Duration="00:00:08" BeginTime="00:00:01" From="10" To="200" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> </Button> </StackPanel> </Window> |
▶ MainWindow.xaml.cs
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 |
using System; using System.Windows; using System.Windows.Media.Animation; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.storyboard.CurrentStateInvalidated += storyboard_CurrentStateInvalidated; this.widthAnimation1.CurrentStateInvalidated += widthAnimation1_CurrentStateInvalidated; this.widthAnimation2.CurrentStateInvalidated += widthAnimation2_CurrentStateInvalidated; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 스토리보드 현재 상태 무효시 처리하기 - storyboard_CurrentStateInvalidated(sender, e) /// <summary> /// 스토리보드 현재 상태 무효시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void storyboard_CurrentStateInvalidated(object sender, EventArgs e) { Clock clock = sender as Clock; this.listBox1.Items.Add($"{clock.CurrentTime} : {clock.CurrentState}"); } #endregion #region 너비 애니메이션 1 현재 상태 무효시 처리하기 - widthAnimation1_CurrentStateInvalidated(sender, e) /// <summary> /// 너비 애니메이션 1 현재 상태 무효시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void widthAnimation1_CurrentStateInvalidated(object sender, EventArgs e) { Clock clock = sender as Clock; this.listBox2.Items.Add($"{clock.CurrentTime} : {clock.CurrentState}"); } #endregion #region 너비 애니메이션 2 현재 상태 무효시 처리하기 - widthAnimation2_CurrentStateInvalidated(sender, e) /// <summary> /// 너비 애니메이션 2 현재 상태 무효시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void widthAnimation2_CurrentStateInvalidated(object sender, EventArgs e) { Clock clock = sender as Clock; this.listBox3.Items.Add($"{clock.Parent.CurrentTime} : {clock.CurrentState}"); } #endregion } } |