WPF custom command commands

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.