Earlier we learn about Mouse & Keyboard input and events. RoutedCommand is used to handle the input of users in a more precise way. Just to simplify the logic RoutedCommand allow to bind shortcuts of keyboard and user define gesture in WPF. It can be bound to all WPF UI components based on enable and disable properties.
Mostly WPF developers used this for menu setup in their applications, like “New”, “Save”, and “Save as” features. Just to get more understanding we are creating an example of the menu at the top of the WPF application with help of a stack panel.
In the following example, you will observe we create a menu and menu item with a command tag, in which we write the command name, like “Properties”.
<MenuItem Header = "Properties" Command = "Properties" />
Then in C# we are binding or routing menu items to the user-defined method. We defined three parameters in bindings.
- The actual command will be performed.
- The method will be performed when the command is executed.
- The status of the command, mean does it allow an execution event or not.
CommandBindings.Add(new CommandBinding(ApplicationCommands.Properties, PropertiesCommandExecuted, AllowProperties));
Now RoutedCommand is configured for properties, whenever someone clicks the properties from the menu it will execute the user define a method on which you will write your business logic.
So basically Routed Commands are used to enable shortcuts to predefined features to override or modified according to user needs.
From the following code, you can get a clear idea of how it will work.
C# Code:
using System.Windows; using System.Windows.Input; namespace JWPF { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); CommandBindings.Add(new CommandBinding(ApplicationCommands.New, NewCommandExecuted, AllowNew)); CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, OpenCommandExecuted, AllowOpen)); CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, SaveCommandExecuted, AllowSave)); CommandBindings.Add(new CommandBinding(ApplicationCommands.Properties, PropertiesCommandExecuted, AllowProperties)); CommandBindings.Add(new CommandBinding(ApplicationCommands.Print, PrintCommandExecuted, AllowPrint)); } private void PrintCommandExecuted(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("You want to print."); } private void AllowPrint(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void PropertiesCommandExecuted(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("You want to open properties."); } private void AllowProperties(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void NewCommandExecuted(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("You want to create new file."); } private void AllowNew(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void OpenCommandExecuted(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("You want to open existing file."); } private void AllowOpen(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void SaveCommandExecuted(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("You want to save a file."); } private void AllowSave(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } } }
XAML Code:
<Window x:Class="JWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:JWPF" mc:Ignorable="d" Title="C# WPF Tutorial - By Junaid Shahid" Height="250" Width="400"> <StackPanel x:Name = "stack" Background = "Transparent"> <Menu> <MenuItem Header = "File" > <MenuItem Header = "New" Command = "New" /> <MenuItem Header = "Open" Command = "Open" /> </MenuItem> <MenuItem Header = "View" > <MenuItem Header = "Properties" Command = "Properties" /> </MenuItem> <MenuItem Header = "Help" > <MenuItem Header = "Print" Command = "Print" /> </MenuItem> </Menu> </StackPanel> </Window>
Output:
For more depth knowledge watch the following video: