基础类,继承与ICommand接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Input; 6 7 namespace WpfExample 8 { 9 public class RelayCommand : ICommand 10 { 11 #region Fields 12 13 /// <summary> 14 /// Encapsulated the execute action 15 /// </summary> 16 private Action<object> execute; 17 18 /// <summary> 19 /// Encapsulated the representation for the validation of the execute method 20 /// </summary> 21 private Predicate<object> canExecute; 22 23 #endregion // Fields 24 25 #region Constructors 26 27 /// <summary> 28 /// Initializes a new instance of the RelayCommand class 29 /// Creates a new command that can always execute. 30 /// </summary> 31 /// <param name="execute">The execution logic.</param> 32 public RelayCommand(Action<object> execute) 33 : this(execute, DefaultCanExecute) 34 { 35 } 36 37 /// <summary> 38 /// Initializes a new instance of the RelayCommand class 39 /// Creates a new command. 40 /// </summary> 41 /// <param name="execute">The execution logic.</param> 42 /// <param name="canExecute">The execution status logic.</param> 43 public RelayCommand(Action<object> execute, Predicate<object> canExecute) 44 { 45 if (execute == null) 46 { 47 throw new ArgumentNullException("execute"); 48 } 49 50 if (canExecute == null) 51 { 52 throw new ArgumentNullException("canExecute"); 53 } 54 55 this.execute = execute; 56 this.canExecute = canExecute; 57 } 58 59 #endregion // Constructors 60 61 #region ICommand Members 62 63 /// <summary> 64 /// An event to raise when the CanExecute value is changed 65 /// </summary> 66 /// <remarks> 67 /// Any subscription to this event will automatically subscribe to both 68 /// the local OnCanExecuteChanged method AND 69 /// the CommandManager RequerySuggested event 70 /// </remarks> 71 public event EventHandler CanExecuteChanged 72 { 73 add 74 { 75 CommandManager.RequerySuggested += value; 76 this.CanExecuteChangedInternal += value; 77 } 78 79 remove 80 { 81 CommandManager.RequerySuggested -= value; 82 this.CanExecuteChangedInternal -= value; 83 } 84 } 85 86 /// <summary> 87 /// An event to allow the CanExecuteChanged event to be raised manually 88 /// </summary> 89 private event EventHandler CanExecuteChangedInternal; 90 91 /// <summary> 92 /// Defines if command can be executed 93 /// </summary> 94 /// <param name="parameter">the parameter that represents the validation method</param> 95 /// <returns>true if the command can be executed</returns> 96 public bool CanExecute(object parameter) 97 { 98 return this.canExecute != null && this.canExecute(parameter); 99 } 100 101 /// <summary> 102 /// Execute the encapsulated command 103 /// </summary> 104 /// <param name="parameter">the parameter that represents the execution method</param> 105 public void Execute(object parameter) 106 { 107 this.execute(parameter); 108 } 109 110 #endregion // ICommand Members 111 112 /// <summary> 113 /// Raises the can execute changed. 114 /// </summary> 115 public void OnCanExecuteChanged() 116 { 117 EventHandler handler = this.CanExecuteChangedInternal; 118 if (handler != null) 119 { 120 //DispatcherHelper.BeginInvokeOnUIThread(() => handler.Invoke(this, EventArgs.Empty)); 121 handler.Invoke(this, EventArgs.Empty); 122 } 123 } 124 125 /// <summary> 126 /// Destroys this instance. 127 /// </summary> 128 public void Destroy() 129 { 130 this.canExecute = _ => false; 131 this.execute = _ => { return; }; 132 } 133 134 /// <summary> 135 /// Defines if command can be executed (default behaviour) 136 /// </summary> 137 /// <param name="parameter">The parameter.</param> 138 /// <returns>Always true</returns> 139 private static bool DefaultCanExecute(object parameter) 140 { 141 return true; 142 } 143 } 144 }
在VM中绑定对应命令的方法
1 public ICommand ToggleExecuteCommand { get;set; }//前台绑定的命令 2 public ICommand HiButtonCommand { get; set; }//前台绑定的命令 3 4 public MainWindowViewModel() 5 { 6 HiButtonCommand = new RelayCommand(ShowMessage,CanExecute2);//初始化命令调用的方法 7 ToggleExecuteCommand = new RelayCommand(ChangeCanExecute);//初始化命令调用的方法 8 } 9 10 private bool CanExecute2(object obj)//调用的方法体函数 11 { 12 return true; 13 } 14 15 public void ShowMessage(object obj)//调用的方法体函数 16 { 17 MessageBox.Show(obj.ToString()); 18 } 19 20 public void ChangeCanExecute(object obj)//调用的方法体函数 21 { 22 // 23 }
时间: 2024-11-25 19:54:30