One, custom commands
The custom command must implement the ICommand interface, as shown in the following code:
<summary>///the Custom Purge command. Barefoot Thinking 2014-7-31 06:51:32///</summary>public class clearcommand:icommand{public bool CanExecute (object Parameter) { throw new notimplementedexception (); } <summary>///// the event should be fired when the command executable state has changed. /// </summary> public event EventHandler canexecutechanged; The <summary> ////command is executed to perform business-related clear logic. //</summary>// <param name= "parameter" > the target object that executes the command. </param> public void Execute (object parameter) { IView view = parameter as IView; if (view! = null) view. Clear (); }}
Second, implement the custom command source
Custom command sources need to be implementedICommandSourceInterface.
<summary>///the custom command source. Tri Youlai 2014-7-31 06:54:26///</summary>public class Mycommandsource:usercontrol, icommandsource{public ICommand Command {get; set;} Public object CommandParameter {get; set;} Public System.Windows.IInputElement Commandtarget {get; set;} protected override void Onmouseleftbuttondown (MouseButtonEventArgs e) { base. Onmouseleftbuttondown (e); Executes the command on the command target. if (this.commandtarget! = NULL && This.command! = null) This.Command.Execute (this.commandtarget);} }
iii. use of custom commands
Clearcommand clearcmd = new Clearcommand (); Mycommandsource1.command = Clearcmd;this. Mycommandsource1.commandtarget = this. MiniView1;
WPF custom command commands