■ TriggerAction<T> 클래스를 사용해 전경색 변경 액션을 만드는 방법을 보여준다.
▶ ChangeForegroundAction.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 83 84 85 86 87 88 |
using System.Windows; using System.Windows.Controls; using System.Windows.Interactivity; using System.Windows.Media; namespace TestProject { /// <summary> /// 전경색 변경 액션 /// </summary> public class ChangeForegroundAction : TriggerAction<TextBlock> { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 전경 브러시 속성 - ForegroundProperty /// <summary> /// 전경 브러시 속성 /// </summary> public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register ( "Foreground", typeof(Brush), typeof(ChangeForegroundAction), new PropertyMetadata(Brushes.Black) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 전경 브러시 - Foreground /// <summary> /// 전경 브러시 /// </summary> public Brush Foreground { get { return (Brush)GetValue(ForegroundProperty); } set { SetValue(ForegroundProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 호출하기 - Invoke(parameter) /// <summary> /// 호출하기 /// </summary> /// <param name="parameter">매개 변수</param> protected override void Invoke(object parameter) { TextBlock textBlock = AssociatedObject as TextBlock; if(textBlock == null || parameter == null) { return; } string parametString = parameter.ToString().ToUpper(); if(parametString == "CHANGE") { textBlock.Foreground = Foreground; } else { textBlock.Foreground = Brushes.Black; } } #endregion } } |
▶ TextChangedTrigger.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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Interactivity; namespace TestProject { /// <summary> /// 텍스트 변경시 트리거 /// </summary> public class TextChangedTrigger : TriggerBase<TextBlock> { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 변경 시간 속성 - ChangeTimeProperty /// <summary> /// 변경 시간 속성 /// </summary> public static readonly DependencyProperty ChangeTimeProperty = DependencyProperty.Register ( "ChangeTime", typeof(string), typeof(TextChangedTrigger), new PropertyMetadata(string.Empty) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변경 시간 - ChangeTime /// <summary> /// 변경 시간 /// </summary> public string ChangeTime { get { return (string)GetValue(ChangeTimeProperty); } set { SetValue(ChangeTimeProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 부착시 처리하기 - OnAttached() /// <summary> /// 부착시 처리하기 /// </summary> protected override void OnAttached() { base.OnAttached(); AssociatedObject.Loaded += AssociatedObject_Loaded; } #endregion #region 탈착시 처리하기 - OnDetaching() /// <summary> /// 탈착시 처리하기 /// </summary> protected override void OnDetaching() { AssociatedObject.Loaded -= AssociatedObject_Loaded; base.OnDetaching(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 결합 객체 로드시 처리하기 - AssociatedObject_Loaded(sender, e) /// <summary> /// 결합 객체 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void AssociatedObject_Loaded(object sender, RoutedEventArgs e) { TextBlock textBlock = AssociatedObject as TextBlock; if(textBlock == null) { return; } DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty ( TextBlock.TextProperty, typeof(TextBlock) ); if(descriptor == null) { return; } descriptor.AddValueChanged(textBlock, textBlock_TextChanged); } #endregion #region 텍스트 블럭 텍스트 변경시 처리하기 - textBlock_TextChanged(sender, e) /// <summary> /// 텍스트 블럭 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void textBlock_TextChanged(object sender, EventArgs e) { TextBlock textBlock = sender as TextBlock; if(textBlock == null) { return; } if(ChangeTime.Contains(textBlock.Text)) { InvokeActions("CHANGE"); } else { InvokeActions(""); } } #endregion } } |
▶ 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TriggerAction<T> 클래스 : 전경색 변경 액션 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Storyboard x:Key="hourStoryboard"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="hourTextBlock" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1.1"> <EasingDoubleKeyFrame.EasingFunction> <SineEase EasingMode="EaseIn" /> </EasingDoubleKeyFrame.EasingFunction> </EasingDoubleKeyFrame> <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"> <EasingDoubleKeyFrame.EasingFunction> <SineEase EasingMode="EaseOut" /> </EasingDoubleKeyFrame.EasingFunction> </EasingDoubleKeyFrame> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="minuteStoryboard"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="minuteTextBlock" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1.1"> <EasingDoubleKeyFrame.EasingFunction> <SineEase EasingMode="EaseIn" /> </EasingDoubleKeyFrame.EasingFunction> </EasingDoubleKeyFrame> <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"> <EasingDoubleKeyFrame.EasingFunction> <SineEase EasingMode="EaseOut" /> </EasingDoubleKeyFrame.EasingFunction> </EasingDoubleKeyFrame> </DoubleAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="secondStoryboard"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="secondTextBlock" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1.1"> <EasingDoubleKeyFrame.EasingFunction> <SineEase EasingMode="EaseIn" /> </EasingDoubleKeyFrame.EasingFunction> </EasingDoubleKeyFrame> <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"> <EasingDoubleKeyFrame.EasingFunction> <SineEase EasingMode="EaseOut" /> </EasingDoubleKeyFrame.EasingFunction> </EasingDoubleKeyFrame> </DoubleAnimationUsingKeyFrames> </Storyboard> </Window.Resources> <Grid> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="FontSize" Value="100" /> </Style> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <!-- 시 --> <TextBlock x:Name="hourTextBlock" Grid.Column="0" RenderTransformOrigin="0.5 1" Text="00"> <TextBlock.RenderTransform> <TransformGroup> <ScaleTransform /> <SkewTransform /> <RotateTransform /> <TranslateTransform /> </TransformGroup> </TextBlock.RenderTransform> <i:Interaction.Triggers> <local:TextChangedTrigger ChangeTime="23"> <ei:ControlStoryboardAction Storyboard="{StaticResource hourStoryboard}" /> <local:ChangeForegroundAction Foreground="Red" /> </local:TextChangedTrigger> </i:Interaction.Triggers> </TextBlock> <TextBlock Grid.Column="1" Text=":" /> <!-- 분 --> <TextBlock x:Name="minuteTextBlock" Grid.Column="2" RenderTransformOrigin="0.5 1" Text="00"> <TextBlock.RenderTransform> <TransformGroup> <ScaleTransform /> <SkewTransform /> <RotateTransform /> <TranslateTransform /> </TransformGroup> </TextBlock.RenderTransform> <i:Interaction.Triggers> <local:TextChangedTrigger ChangeTime="10 12 14 16"> <ei:ControlStoryboardAction Storyboard="{StaticResource minuteStoryboard}"/> <local:ChangeForegroundAction Foreground="Green" /> </local:TextChangedTrigger> </i:Interaction.Triggers> </TextBlock> <TextBlock Grid.Column="3" Text=":" /> <!-- 초 --> <TextBlock x:Name="secondTextBlock" Grid.Column="4" RenderTransformOrigin="0.5 1" Text="00"> <TextBlock.RenderTransform> <TransformGroup> <ScaleTransform /> <SkewTransform /> <RotateTransform /> <TranslateTransform /> </TransformGroup> </TextBlock.RenderTransform> <i:Interaction.Triggers> <local:TextChangedTrigger ChangeTime="10 20 30 40 50"> <ei:ControlStoryboardAction Storyboard="{StaticResource secondStoryboard}" /> <local:ChangeForegroundAction Foreground="Blue" /> </local:TextChangedTrigger> </i:Interaction.Triggers> </TextBlock> </Grid> </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 64 65 66 |
using System; using System.Windows; using System.Windows.Threading; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; timer.Tick += timer_Tick; timer.Start(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 타이머 틱 처리하기 - timer_Tick(sender, e) /// <summary> /// 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void timer_Tick(object sender, EventArgs e) { if(DateTime.Now.Hour > 12) { this.hourTextBlock.Text = (DateTime.Now.Hour - 12).ToString("D2"); } else { this.hourTextBlock.Text = DateTime.Now.Hour.ToString("D2"); } this.minuteTextBlock.Text = DateTime.Now.Minute.ToString("D2"); this.secondTextBlock.Text = DateTime.Now.Second.ToString("D2"); } #endregion } } |