Composite application Guidance for WPF (9)--command

Source: Internet
Author: User

Commands in command mode are used here, and almost every application has that pattern, how to "copy" "Paste" "undo" and so on. We know that this pattern separates the requestor of the operation from the execution logic of the operation, and it has good support for the operation of the request queue and undo repetition, so it is widely used. WPF has further encapsulated and improved it so that WPF programs can easily use commands and build custom commands, and the dozens of of common commands built into WPF and the Advanced Command routing mode (Routed) make it all so easy and efficient.

So is the WPF command (RoutedCommand and Routeduicommand) enough to fix everything? Not so, in fact, it has a lot of deficiencies, which is why prism to build a set of their own command reasons. This article will briefly explore these issues.

-------------------------------the Command----------------------------for WPF

1, understanding the "Routed" (routing) in the WPF RoutedCommand

WPF provides the RoutedCommand and Routeduicommand two commands, where Routeduicommand inherits from RoutedCommand and opens MSDN, and we can see this explanation "it defines an implementation ICommand And the commands that are routed through the element tree, the Execute and CanExecute methods on the RoutedCommand do not contain the application logic of the command (for example, a typical icommand is the case), but instead will throw events that traverse the element tree to find The object of the commandbinding. An event handler that is attached to a commandbinding contains command logic. "This is called" Routing ": It looks for commandbinding on the path of a particular element tree (actually the visual tree) and then invokes Commandbinding's CanExecute and execute to determine whether it can be executed and how to execute the command. So what is the path, see the following example:

  

The buttons in the above two images are shaped like

<button command= "Copy" content= "{Binding path=command.text, Relativesource={relativesource Self}"/>

First, when the text in the text box (textbox) is selected, the Copy button on the toolbar is enabled and the button to the right of the text box is not enabled;

In the second picture, when the text in the FlowDocument is selected, the buttons in the FlowDocument and the hungry buttons in the toolbar are enabled. The buttons next to the text box are never enabled.

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.