BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Commanding - Using Application Commands

The one hundred and sixty-fourth part of the Windows Presentation Foundation Fundamentals tutorial begins a look at commanding. This allows reusable commands to be linked to controls, providing separation of user interface and functionality.

Commands

If you have previously developed software using event-driven environments, such as Windows Forms, you will be comfortable with the use of events that we have seen with WPF. Although routed events add extra features, their use is very similar to the standard events used elsewhere.

When using events, you register an event handler against a control, linking it to a method behind the window. This works well but encourages programmers to embed the code in the window's class, making it harder to reuse and difficult to execute from within automated unit testing frameworks.

WPF adds a more powerful and flexible way to react to user actions using commanding. A command is a self-contained, reusable unit of code that performs some functionality. They do not need to be coded within the window's class. Instead, you can instantiate them from within a linked data context and connect them to controls using command binding, which is similar to data binding. They can be linked to controls that support commands, such as menus, toolbars and buttons, and to keyboard combinations. This allows true separation of the user interface from application logic.

In addition to containing the logic to run when a command is executed, commands can include code to understand when they can be used. For example, you might create a save command that can only be started when some entered information is valid. When the data is invalid, the command could automatically indicate that it cannot be used. Buttons and other controls linked to such a disabled command are automatically disabled accordingly.

There are two ways to use commands. In this article we will use commands that are provided by the .NET framework's base class library. There are lots of commands that perform standard operating system features, which you can reuse instead of recreating them. However, their functionality is limited. In the next article we'll see how to create custom commands.

The built-in commands that we will use are all accessed via static properties of the ApplicationCommands class. These properties return RoutedUICommand objects, which include additional intelligence, including providing standard text to use in controls and default shortcut keys.

To begin, create a new WPF application in Visual Studio named, "CommandingDemo". Once the solution is prepared, replace the XAML in the main window with the following code:

<Window x:Class="CommandingDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="300">
    
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_Edit">
            </MenuItem>
        </Menu>
        
        <TextBox TextWrapping="Wrap" AcceptsReturn="True"
                 VerticalScrollBarVisibility="Visible"/>
    </DockPanel>
</Window>

The window uses a DockPanel to control the layout of a menu bar and a TextBox. It should appear similar to the image below.

WPF basic commanding demo window

10 June 2015