WPF has Menuand ToolBar controls that serve the same purpose as the controls you know from Windows Forms: to start commands. With these controls you could add event handlers to fulfill the
functionality of the commands. However, you can start commands by selecting menus, clic1cingtoolbar buttons, or by pressing some special keys on the keyboard. To handle all these different input gestures, WPF supplies another feature: commands.
Some of the WPF controls offer an implementation for predefined commands that make it extremely easy to get to some functionality.
WPF offers some predefined commands with the commands classes ApplicationCommands, Edi tingCommands, ComponentCommands,and NavigationCommands. All these commands classes are static classes with static properties that return RoutedUICommandobjects. For example, some of the ApplicationCommands properties are New,Open, Save, SaveAs, Print, and Close – commands you know from many applications.
To get started with commands, create a simple WPF project and add a Menucontrol with items for undo .” and redo and cut, copy, and paste. The TextBox named textContent takes the remaining space of the Windowand allows for multiline user input. Within the window a DOCKPanelis created to define the layout. Docked on top you can find the Menucontrol with MenuItern elements. The header is set to define the text of the menu. The _ (underscore) defines the letter that can be accessed directly with the keyboard without using the mouse. When you press the Alt key, the underscore is shown below the letter that follows in the header text. The Commandproperty defines the command associated with the menu item.
That’s all you need to do for clipboard functionality. The TextBOXf’class already includes functionality for these predefined command bindings. Starting the application, when you enter text in the text box you can see possible menu items enabled. Selecting text in the text box makes the cut and copy menu items available.
Now the application is going to be changed to add command bindings that are not previously defined with the controls. Commands to open and save a me are added to the editor.
To make the commands accessible, more MenuItem elements are added to the Menu element as shown:
The commands can also be accessed from a toolbar. With the ToolBar element the same commands that are available from the menu are defined. For arranging the toolbar, the ToolBar element is placed within a ToolBarTray:
Now command bindings must be defined to associate the commands to event handlers. Command bindings can be assigned to any WPF class that derives from the base class UIElement that is very high in the hierarchy. Command bindings are added to the CommandBindings property by defining CommandBinding elements. The CommandBinding class has the property Command where you can specify an object implementing the ICommand interface, and the events CanExecute and Executed to specify event handlers. Here the command bindings are assigned to the window class. The Executed event is set to the event handler methods that implement the functionality behind the commands. If a
command should not be available at all times, you can set the CanExecute event to a handler that decides if the command should be available.
In the code behind the handler method, NewFileExecuted () empties the text box and writes the file name unti tIed. txt to the Ti t Le property of the Window class. In OpenFileExecuted () the Microsoft. Win32. OpenFileDialog is created and shown as a dialog. With a successful exit of the dialog, the selected file is opened and its content is written to the TextBox control.
A dialog for opening a file is no~’predefined in WPF. You can also create a custom window for selecting files and folders, or you can use the OpenFil.Dialog clUB from the Microsoft _Win32 namespace that is a wrapper around the new Windows dialog_,
The handler SaveFileCanExecute () returns the decision as to whether the command to save the file should be available depending on if the content has been changed:
The application with the opened file sample. txt is shown.