Bind commands to commands in WPF (2)

Source: Internet
Author: User

Bind commands to commands in WPF (2)
Zhou yinhui

In WPF, commands (commanding) are divided into four parts: icommand, icommandsource, commandtarget, and commandbinding. Next we will discuss these four parts separately.

1, icommand
Command is our "command" itself, such as "copy" and "Paste ". In WPF, all commands must implement the icommand interface, which provides an abstraction for all commands. This abstraction is very important for us to implement undo and redo operations, if you learn the "command" mode in the design mode, you will have a deeper understanding.
The icommand interface has the execute () method, which is used for command execution (however, note: command Execution logic-for example, to put the text in the clipboard out to a proper position in the text box-is not written into this method. We will talk about the mysteries of this method later ), another method is canexecute () to indicate whether the current command is available on the target element. When this availability changes, it will trigger an event canexecutechanged at the end of the interface.
In the current WPF class library, you can see the only routedcommand that implements the icommand interface (in fact, another class named secureuicommand also implements this interface, but this class has not been made public ), "routed" is a modifier that is not easily translated (someone translates it into "routing "), however, this means that this type of command can be passed up and down in the element tree like routedevent in WPF.
Routeduicommand, a subclass of routedcommand, is a frequently used type. Its difference with routedcommand is that it only has a text attribute to describe the command, however, the text attribute of most embedded WPF commands has a very good feature: it supports automatic localization. This will at least reduce the workload for localization of our software.

The subsequent sections of this series will show you how to customize a command.

2. icommandsource and commandtarget
Command source, used to trigger our command. For example, if you use a menu item to trigger the "copy" command, this menu item is the command source. To make an element a command source, it must implement the icommandsource interface. The command source determines the command to be triggered, the object to be triggered by the command, and the command parameters (if needed), which correspond to the three attributes of the command: command, commandtarget, and commandparameter. Note commandtarget, because in WPF, if you do not specify its command object for the command source, the elements that obtain the keyboard focus on the interface will be used as the default command object, this provides us with convenience. For example, there are two text boxes on the interface, so we don't have to worry about which text box the "Paste" Operation on the main menu item is for, and who gets the focus is, this is in line with everyone's habits. However, the problem is that if the Command target does not have the ability to obtain the keyboard focus (such as label) or the command source will seize the focus (such as replacing the menu item with a button, when you click the button, the focus is shifted from the text box to the button. Your command will be invalid. In this case, you must specify the Command target for the command source.

In the subsequent sections of this series, we will introduce how to make your custom controls a command source and a command target.

3, commandbinding
As mentioned above, we have not compiled the command execution logic into its excute () method. This makes sense, for example, "Paste" command (applicationcommands. paste), paste a piece of text to the text box and paste an image to the drawing board the execution logic is certainly not the same, developers responsible for developing the "Paste" command cannot know the specific logic of all the paste operations. Customers who use the "Paste" command should not be responsible for the execution logic, tasks that write the execution logic should be distributed to developers who support the "paste" operation and those who want to add the "paste" operation for their own controls. That is to say, we need to separate "behavior requestor (command)" from "behavior Executor (command execution logic)" to implement a loose coupling, while commandbinding (command binding) is the bridge between command and command execution logic.
We use commandbinding to bind the command with its appropriate execution logic:

Commandbinding closecommandbinding =   New Commandbinding (
Applicationcommands. Close, closecommandhandler, canexecutehandler );

The last two parameters of the commandbinding constructor are executedroutedeventhandler and canexecuteroutedeventhandler, which are used to indicate how to execute commands and how to determine whether a command can be executed.
Like commandbinding, The commandmanager class plays an intermediate role. It provides many practical methods for command binding (and input binding.

Later in this series, we will introduce the relationship between the WPF command system and the "command mode" (one of the design modes.

 

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.