■ ICommand 인터페이스를 사용해 릴레이 명령을 만드는 방법을 보여준다.
▶ RelayCommand.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 |
using System; using System.Windows.Input; /// <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 } |