In WPF, many controls automatically integrate an inherent set of commands. For example, text box textbox provides copy (copy), paste (Paste), crop (cut), Undo and Redo (Redo) commands, and so on.
WPF provides a set of commands used by common applications, including:Applicationcommands, Componentcommands, Navigationcommands, Mediacommands and EditingCommands.
applicationcommands (Application command): cancelprint: Cancel printing close: Close contextmenu: Context Menu copy: Copy correctionlist:gets The value that represents the Correction List command. cut: Cut delete: Delete find: Find help: Help new: New notacommand: Not a command, Ignored open: Open paste: Paste print: Print printpreview: Print Preview properties: Properties redo: Redo Replace: Replace save: Saving saveas: Save As selectall: Select all stop: Stop undo: Undo
componentcommands (Component command): ExtendSelection: followed by down/left/right/up, for example: Extendselectiondown (shift+down,extend Selection down), Extendselectionleft, such as Move: After the down/left/right/up, such as: MoveDown Movefocus: followed by down/forward/back/up, such as: Movefocusdown Movefocuspage: followed by down/up, such as: Movefocuspageup MoveTo: followed by End/home/pagedown/pageup, for example: Movetopagedown scrollbyline Scrollpage: followed by down/left/right/up, such as: Scrollpageleft Selectto:end/home/pagedown/pageup, for example: SelectToEnd
navigationcommands (Navigation command): Browse Browsing: followed by back/forward/home/stop, such as: Browseback Zoom display: decreasezoom, increasezoom, Zoom Favorites (Favorites) page: FirstPage, La Stpage, PreviousPage, nextpage,gotopage navigatejournal Refresh (refreshing) search (search)
mediacommands (Multimedia control command): Treble treble: Decreasetreble,increasetreble bass bass: Boostbass,decreasebass,increasebass Channel channel: Channeldown, Channelup microphonevolume Microphone Volume adjustment: Decreasemicrophonevolume,increasemicrophonevolume,mutemicrophonevolume Togglemicrophoneonoff: Mic Switch Volume Volume: Decreasevolume,increasevolume,mutevolume Rewind, Fastforward (playback, Fast forward) track tracks: Previoustrack,nexttrack [Previous (section)] Play,pause,stop,record (play, pause, stop, record) Toggleplaypause Select selection
editingcommands (Edit/typeset commands): align Alignment: Aligncenter,alignjustify,alignleft,alignright (center, full, left-aligned, right-aligned) backspace backspace tabforward, Tabbackward (tab Front indent, tab backward) fontsize font size: decreasefontsize,increasefontsize Indentation indent: decreaseindentation, increaseindentation delete Delete: Delete selected, Deletenextword: Remove the next word, Deletepreviousword: Delete previous word enterlinebreak: line break enterparagraphbreak: Change Segment correctspellingerror/ Ignorespellingerror: correcting/ignoring spelling errors moveupbyline,movedownbyline: Move Up/down one line, moveupbypage,movedownbypage: Move Up/down a page moveupbyparagraph,movedownbyparagraph: Move Up/down a section of moveleftbycharacter/moverightbycharacter: Left/Right Move one character Moveleftbyword/moverightbyword left/Right Move word movetodocumentstart/movetodocumentend: to the beginning/end of the article movetolinestart/ Movetolineend: To the beginning/end of a line selectupbyline,selectdownbyline: Up/down Select a line selectupbypage,selectdownbypage: Up/ Select a page selectupbyparagraph,selectdownbyparagraph: Up/down Select a section of selectleftbycharacter,selectrightbycharacter: Left/Right Select a word selectleftbyword,sElectrightbyword: Select the word left/right selecttodocumentstart,selecttodocumentend: Select to Header/end of article selecttolinestart/ Selecttolineend: Check to beginning/end of line togglebold, Toggleitalic, Toggleunderline (bold, italic, underline) togglebullets, Togglenumbering (list: Add, add number) toggleinsert: Insert togglesuperscript,togglesubscript (superscript, subscript)
Let's give a simple example:
XAML code: <StackPanel> <Menu> <menuitem command= "Applicationcommands.paste" /> </Menu> <textbox/> </StackPanel>
C # code: StackPanel Mainstackpanel = new StackPanel (); TextBox Pastetextbox = new TextBox (); Menu Stackpanelmenu = new menu (); MenuItem Pastemenuitem = new MenuItem ();
STACKPANELMENU.ITEMS.ADD (Pastemenuitem); MAINSTACKPANEL.CHILDREN.ADD (Stackpanelmenu); MAINSTACKPANEL.CHILDREN.ADD (Pastetextbox);
Pastemenuitem.command = Applicationcommands.paste;
The code above shows that when the text box is set to focus, the menu item is available, and the Paste command is executed when you click the button.
The four concepts and four minor questions about command are listed below: 1. Four Concepts of command (commands) in WPF: (1) Command: The action to be performed. (2) Command Source: The object that issued the command (inherited from ICommandSource). (3) command target: The body that executes the command (4) commands bind command binding: objects that map command logic like in the example above, paste (Paste) is command, menu item ( MenuItem) is a command source, a text box (textbox) that is a command target object, which is bound to a command binding text box (textbox) control.
Tips: Commands in WPF are inherited from the ICommand interface. ICommand exposed Two methods: Execute method, CanExecute method, and an event: canexecutechanged. Inherited from ICommandSource are: ButtonBase, MenuItem, Hyperlink and inputbinding. And Button,gridviewcolumnheader,togglebutton,repeatbutton inherits from ButtonBase. System.Windows.Input.KeyBinding and mousebinding inherit from InputBinding.
2, four small problems: (1) How to specify the command Sources? XAML: ( Replace "applicationcommands.properties" with the corresponding Applicationcommands attribute value, such as: Applicationcommands.copy) < stackpanel> <StackPanel.ContextMenu> <ContextMenu> <menuitem command= "applicationcom Mands. Properties " /> </ContextMenu> </StackPanel.ContextMenu> </StackPanel>
equivalent C # code: StackPanel Cmdsourcepanel = new StackPanel (); ContextMenu Cmdsourcecontextmenu = new ContextMenu (); MenuItem Cmdsourcemenuitem = new MenuItem ();
Cmdsourcepanel.contextmenu = Cmdsourcecontextmenu; CMDSOURCEPANEL.CONTEXTMENU.ITEMS.ADD (Cmdsourcemenuitem);
Cmdsourcemenuitem.command = Applicationcommands.properties;
(2) How do I specify shortcut keys?
XAML code: <Window.InputBindings> <KeyBinding key= "B" modifiers= "Control" command= "Applicationcommands.open"/> </Window.InputBindings>
C # code: KeyGesture openkeygesture = new KeyGesture ( key.b, Modifierkeys.control);
KeyBinding opencmdkeybinding = new KeyBinding (applicationcommands.open,openkeygesture); Inputbindings.add (opencmdkeybinding); You can also do this (the following sentence is equivalent to the effect of the above two sentences): //applicationcommands.open.inputgestures.add (openkeygesture); (3) How to command Binding? XAML code: <window.commandbindings> < Commandbinding command= "Applicationcommands.open" executed= "opencmdexecuted" canexecute= "Opencmdcanexecute"/ > </window.commandbindings>
C # code: commandbinding opencmdbinding = new Commandbinding (Applicationcommands.open, opencmdexecuted, OpenCmdCanE Xecute);
THIS.COMMANDBINDINGS.ADD (opencmdbinding);
specific event handling: C # code: void opencmdexecuted(object target, Executedroutedeventargs e) {MessageBox.Show ("the Comm and has been invoked. ");}
void opencmdcanexecute(object sender, Canexecuteroutedeventargs e) {E.canexecute = true;}
(4) How to set command target and bind command binding? XAML code: <StackPanel> <Menu> <menuitem command= "Applicationcommands.paste" Comm andtarget= "{Binding elementname=maintextbox}"/> </Menu> <textbox name= "maintextbox "/> </StackPanel> C # code: StackPanel mainstackpanel = new StackPanel (); TextBox Maintextbox= new TextBox (); Menu Stackpanelmenu = new menu (); MenuItem Pastemenuitem = new MenuItem ();
STACKPANELMENU.ITEMS.ADD (Pastemenuitem); MAINSTACKPANEL.CHILDREN.ADD (Stackpanelmenu); MAINSTACKPANEL.CHILDREN.ADD (Maintextbox);
Pastemenuitem.command = Applicationcommands.paste;
The above example is all a single command binding situation, in fact, you can also be more than one button to bind multiple commands to the same control, such as:<stackpanel xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http// Schemas.microsoft.com/winfx/2006/xaml "orientation=" Horizontal "height=" > <buttoncommand= "Cut"commandtarget= "{Binding elementname=Textboxinput} "content=" {Binding relativesource={relativesource self}, Path=command.text} "/> <buttoncommand= "Copy"commandtarget= "{Binding elementname=Textboxinput} "content=" {Binding relativesource={relativesource self}, Path=command.text} "/> <button&NBSP; command= "Paste" commandtarget= "{Binding elementname= Textboxinput }" content= "{Binding relativesource={ RelativeSource self}, Path=command.text} "/> <button&NBSP; Command= "Undo" commandtarget= "{Binding elementname= Textboxinput } "content=" {Binding relativesource={relativesource self}, Path=command.text} "/> <button &NBSP; command= "Redo" commandtarget= "{Binding elementname= Textboxinput }" content= "{Binding relativesource={ RelativeSource self}, Path=command.text} "/> <textbox x:name=" textboxinput "width="/> </stackpanel>
Finally, an example of a complete point is posted:
XAML Code:<window xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http// Schemas.microsoft.com/winfx/2006/xaml "x:class=" Wpfcommand.window1 "title=" menuitemcommandtask "x:Name=" window " Width= "height=" > <Window.CommandBindings> <commandbinding command= "Applicationcommands.open" Executed= "opencmdexecuted"Canexecute="Opencmdcanexecute"/><commandbinding command= "Help" canexecute= "Helpcanexecute" executed= "helpexecuted"/> </Window.CommandBindings><Window.InputBindings> <keybinding command= "Help" key= "F2" /> < KeyBinding command= "Notacommand" key= "F1" /> </Window.InputBindings> <Canvas> <menu dockpanel.dock= "Top" > <menuitem&NBSP; command= "Applicationcommands.paste" width="/> </Menu> < TextBox borderbrush= "Black" borderthickness= "2" textwrapping= "Wrap" text= "This textbox does not become the focus until the Paste menu is unavailable. "Width=" 476 "height=" "canvas.left=" 8 "canvas.top=" "/> <button&NBSP; command=" Applicationcommands.open " height= "+" width= "223" content= "Test Popup Dialog" canvas.left= "8" Canvas.Top= "70 "/> </Canvas> </window>
corresponding C # code: using System; Using System.IO; Using System.Net; Using System.Windows; Using System.Windows.Controls; Using System.Windows.Data; Using System.Windows.Input; Using System.Windows.Media; Using System.Windows.Media.Animation; Using System.Windows.Navigation;
namespace Wpfcommand {Public ' partial class Window1 {public Window1 ()} {this. InitializeComponent (); } void opencmdexecuted(object target, Executedroutedeventargs e) {Messagebox.sho W ("Test Popup dialog box, command executed!") "); }
void&NBSP; Opencmdcanexecute (object sender, Canexecuteroutedeventargs e) { E.canexecute = true;
void&NBSP; helpcanexecute (object sender, Canexecuteroutedeventargs e) { E. CanExecute = true;
void helpexecuted(object sender, Executedroutedeventargs e) {System.Diagnostics.Process. Start ("http://www.BrawDraw.com/"); } } }
you might want to try it after the program executes, press F1 or F2 to try the effect, or not press F2 when the browser points to " http://www.BrawDraw.com/", while pressing F1 does not have any effect? This is because of these two sentences: <keybinding command= "Help" key= "F2"/> <keybinding command= "Notacommand" key= "F1"/> When pressing F2, the help command Execute when F1 is pressed, because command= "Notacommand", the window ignores the execution of this command.
Reproduced in: http://blog.csdn.net/johnsuna/article/details/1770602
Applicationcommands is used to represent common commands that application programmers often encounter, similar to CTRL + C