Using routed commands in a WPF application
Routed commands are used to navigate a route through the element hierarchy. This process is also well known as bubbling and tunneling. The class RoutedCommand
implements the ICommand
interface and allows the attaching of input gestures, such as mouse input and keyboard shortcuts, to the target.
In this recipe, we will learn how to use routed commands with a simple example.
Getting ready
To work on this recipe, we will be using the previous MVVM demo application. Launch your Visual Studio IDE and open the project CH07.MVVMDemo
. In this example, we will be using RoutedCommand
for the Add
button click event.
How to do it...
Follow these simple steps to register the routed command to the button click and perform the operation:
- From the
Solution Explorer
, right-click on the project node and create a folder namedCommands
.
- Right-click on the
Commands
folder and create a new class namedRoutedCommands.cs
by following theAdd
|Class...
context menu path. - Inside...