■ EventTrigger 엘리먼트에서 InvokeCommandAction 객체를 사용해 이벤트 명령을 실행하는 방법을 보여준다.
▶ ReplayCommand.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 |
using System; using System.Windows.Input; namespace TestProject { /// <summary> /// 릴레이 명령 /// </summary> public class RelayCommand : ICommand { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 실행 가능 여부 변경시 - CanExecuteChanged /// <summary> /// 실행 가능 여부 변경시 /// </summary> public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 실행 액션 /// </summary> private readonly Action<object> executeAction; /// <summary> /// 실행 가능 여부 프레디킷 /// </summary> private readonly Predicate<object> canExecutePredicate; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - RelayCommand(executeAction, canExecutePredicate) /// <summary> /// 생성자 /// </summary> /// <param name="executeAction">실행 액션</param> /// <param name="canExecutePredicate">실행 가능 여부 프레디킷</param> public RelayCommand(Action<object> executeAction, Predicate<object> canExecutePredicate) { if(executeAction == null) { throw new ArgumentNullException("executeAction"); } this.executeAction = executeAction; this.canExecutePredicate = canExecutePredicate; } #endregion #region 생성자 - RelayCommand(executeAction) /// <summary> /// 생성자 /// </summary> /// <param name="executeAction">실행 액션</param> public RelayCommand(Action<object> executeAction) : this(executeAction, null) { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 실행하기 - Execute(parameter) /// <summary> /// 실행하기 /// </summary> /// <param name="parameter">파라미터</param> public void Execute(object parameter) { this.executeAction(parameter); } #endregion #region 실행 가능 여부 구하기 - CanExecute(parameter) /// <summary> /// 실행 가능 여부 구하기 /// </summary> /// <param name="parameter">파라미터</param> /// <returns>실행 가능 여부</returns> public bool CanExecute(object parameter) { return this.canExecutePredicate == null ? true : this.canExecutePredicate(parameter); } #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 |
<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="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" Width="800" Height="600" Title="EventTrigger 엘리먼트 : InvokeCommandAction 객체를 사용해 이벤트 명령 실행하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Button Name="button" Width="100" Height="30" Content="테스트"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseWheel"> <i:InvokeCommandAction Command="{Binding WheelCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </Button> </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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using System.Windows; using System.Windows.Input; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// WHEEL 명령 /// </summary> private RelayCommand wheelCommand; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region WHEEL 명령 - WheelCommand /// <summary> /// WHEEL 명령 /// </summary> public ICommand WheelCommand { get { return this.wheelCommand ?? (this.wheelCommand = new RelayCommand(ExecuteWheelCommand, CanExecuteWheelCommand)); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); DataContext = this; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region WHEEL 명령 실행하기 - ExecuteWheelCommand(parameter) /// <summary> /// WHEEL 명령 실행하기 /// </summary> /// <param name="parameter">매개 변수</param> private void ExecuteWheelCommand(object parameter) { MessageBox.Show("마우스 휠을 움직였습니다."); } #endregion #region WHEEL 명령 실행 가능 여부 구하기 - CanExecuteWheelCommand(parameter) /// <summary> /// WHEEL 명령 실행 가능 여부 구하기 /// </summary> /// <param name="parameter">매개 변수</param> /// <returns>WHEEL 실행 가능 여부</returns> private bool CanExecuteWheelCommand(object parameter) { return true; } #endregion } } |
※ 제한 사항 : 해당 이벤트 인자를 전달할 수 없다.