■ VisualStateManager 엘리먼트를 사용하는 방법을 보여준다.
▶ 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 |
<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="VisualStateManager 엘리먼트 사용하기"> <Grid> <Rectangle Name="rectangle" Width="100" Height="100"> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="MouseStates"> <VisualState Name="MouseEnter"> <Storyboard> <ColorAnimation Storyboard.TargetName="rectangleBrush" Storyboard.TargetProperty="Color" To="Green" /> </Storyboard> </VisualState> <VisualState Name="MouseLeave" /> <VisualStateGroup.Transitions> <VisualTransition To="MouseLeave" GeneratedDuration="00:00:00" /> <VisualTransition To="MouseEnter" GeneratedDuration="00:00:00"> <VisualTransition.GeneratedEasingFunction> <ExponentialEase EasingMode="EaseOut" Exponent="10" /> </VisualTransition.GeneratedEasingFunction> </VisualTransition> </VisualStateGroup.Transitions> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Rectangle.Fill> <SolidColorBrush x:Name="rectangleBrush" Color="Red" /> </Rectangle.Fill> </Rectangle> </Grid> </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 |
using System.Windows; using System.Windows.Input; using System.Windows.Shapes; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.rectangle.MouseEnter += rectangle_MouseEnter; this.rectangle.MouseLeave += rectangle_MouseLeave; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 사각형 마우스 진입시 처리하기 - rectangle_MouseEnter(sender, e) /// <summary> /// 사각형 마우스 진입시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void rectangle_MouseEnter(object sender, MouseEventArgs e) { VisualStateManager.GoToElementState(sender as Rectangle, "MouseEnter", true); } #endregion #region 사각형 마우스 이탈시 처리하기 - rectangle_MouseLeave(sender, e) /// <summary> /// 사각형 마우스 이탈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void rectangle_MouseLeave(object sender, MouseEventArgs e) { VisualStateManager.GoToElementState(sender as Rectangle, "MouseLeave", true); } #endregion } } |