Bind commands to commands in WPF (1)

Source: Internet
Author: User
Bind commands to commands in WPF (1)
Zhou yinhui

When it comes to user input, we may think more of the keyboard, mouse, and pen. In fact, we also use a high-level input command (commands ), from the perspective of the WPF class library, they respectively apply to keyboard, mouse, ink, and icommand. A command is a semantic-level input rather than a device-level input, such as "copy" and "Paste". However, there are many methods to implement a command, such as "Paste ", we can use CTRL-V, or the main menu or right-click menu (context menu) and so on. In the past. in the. NET version, it is very troublesome to add a "Paste" button on the software interface, you have to monitor whether the Clipboard contains available text and whether the corresponding text box gets focus to enable or disable this button, when pasting, You have to obtain the corresponding text from the clipboard and insert it to a reasonable position in the text box.

The command mechanism provided in WPF is very simple to implement these tasks. The demo below demonstrates how to simply not manually write a line of background Logic Code To solve the problem above, you can paste the following code to xamlpad:< Window
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
X: Name = "Window"
Title = "Window1"
Width = "640" Height = "480" >

< Dockpanel Lastchildfill = "True" >
< Menu Width = "Auto" Height = "20" Dockpanel. Dock = "TOP" >
< Menuitem Command = "Applicationcommands. Copy" Header = "{Binding Path = command. Text, relativesource = {relativesource self }}" />
< Menuitem Command = "Applicationcommands. Paste" Header = "{Binding Path = command. Text, relativesource = {relativesource self }}" />
< Menuitem Command = "Applicationcommands. Cut" Header = "{Binding Path = command. Text, relativesource = {relativesource self }}" />
< Menuitem Command = "Applicationcommands. Redo" Header = "{Binding Path = command. Text, relativesource = {relativesource self }}" />
< Menuitem Command = "Applicationcommands. Undo" Header = "{Binding Path = command. Text, relativesource = {relativesource self }}" />
</ Menu >
< RichTextBox >
< Flowdocument >
< Paragraph />
</ Flowdocument >
</ RichTextBox >
</ Dockpanel >
</ Window >


In the demo, menu items in the menu bar can not only complete tasks perfectly, but also be automatically enabled and disabled based on the status of the text box and clipboard, however, we did not write any background code for these complex logic. This is the convenience provided by the command mechanism in WPF.

Note this line of code:

< Menuitem Command = "Applicationcommands. Copy" Header = "{Binding Path = command. Text, relativesource = {relativesource self }}" />

We will copy the command (applicationcommands. copy) is assigned to the command attribute of the menu item, and all elements that implement the icommandsource interface have this attribute, which means that this element can be used as a "command source" to trigger a command, its command attribute indicates the command that will be triggered.

In fact, a command system is divided into four parts:
Command: a semantic-level input, such as "copy", "Left alignment", and "play ".
Commandsource: the element that triggers a command, such as buttons, menu items, keyboard (CTRL-C, F1, etc.), and mouse.
Commandtarget: the target to which a command is used, such as a text box or player.
Commandbinding: used to link the processing logic of commands and commands, for example, the same "Paste", but the processing logic of pasting text and pasting images is different, command binding connects the "Paste" command with a reasonable processing logic.
The command system willArticleBut it is worth mentioning that in the demo above, we only specify the command and command source, but not the Command target, however, it uses the element that gets the keyboard focus (here our RichTextBox) as the default value, and the command binding and command background execution logic are hidden inside RichTextBox, developers who write the RichTextBox Control will write this part of code for us.

In addition, you may have discovered that in the demo, we did not directly set "copy" and "Paste" text for the menu item title, but used the following binding:

Header = "{binding Path = command. Text, relativesource = {relativesource self}"/>

We bind the menu text to the text attribute of the command. This is because if a command is of the routeduicommand type, the command has a text attribute to indicate the text name corresponding to the command, the text attribute is automatically localized. That is to say, if your computer uses Simplified Chinese, the menu item displays "copy ", if your computer uses English, the menu item displays "copy ".

WPF provides us with a large number of built-in commands, including applicationcommands, navigationcommands, mediacommands, editingcommands and componentcommands, and control developers also provide many unique commands for their controls (such as slider. decreaselarge and slider. decreasesmall), which is sufficient for most applications in normal times. If not, you can customize more commands for your own applications.

In the subsequent sections of this article, we will further discuss the WPF command system. Please pay attention to it. Thank you.

 

 

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.