> ## Content Index
> Fetch the complete content index at: https://blog.jsinh.in/llms.txt
> Use this file to discover other available public pages before exploring further.

# Understanding WPF Commands
- URL: https://blog.jsinh.in/understanding-wpf-commands/
- Published: 2012-03-05T01:07:04.000Z
- Updated: 2026-07-07T15:18:08.000Z
- Author: Jsinh
- Tags: #Import 2026-10-04 19:16

Lately Microsoft have been using some of the well known design patterns to ease and furnish work of a programmer who choose to bang keyboards using .NET.

One good example is **WPF commands**.

Microsoft at MSDN on [Commanding](https://msdn.microsoft.com/en-us/library/ms752308.aspx?ref=blog.jsinh.in):

> Commanding is an input mechanism in Windows Presentation Foundation (WPF) which provides input handling at a more semantic level than device input.

It resembles and is inspired by the famous [**Command Design Pattern**](http://www.dofactory.com/Patterns/PatternCommand.aspx?ref=blog.jsinh.in) with Microsoft flavors added to it. This **Routed command system** as named by Microsoft is a modified version of the original command design pattern with routing capabilities. It is used to send command and / or behavioral notifications from UI controls. They are analogous to the **Routed event system** in WPF.

The routed command system in WPF has two types of commands:

- [Routed Commands](https://msdn.microsoft.com/en-us/library/system.windows.input.routedcommand.aspx?ref=blog.jsinh.in)
- [Routed UI Commands](https://msdn.microsoft.com/en-us/library/system.windows.input.routeduicommand.aspx?ref=blog.jsinh.in)

It becomes clear from the name itself that the first is used to send and trigger normal command but the later is more related and associated with the user interface. One basic thing that is common to both is that they interface from [ICommand](https://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx?ref=blog.jsinh.in) which defines contracts for a command.

It has two methods `CanExecute`, `Execute` and an event handler `CanExecuteChanged`. `CanExecute` defines if the command can be executed or not and `Execute` defines the action for command behavior.

WPF provides some built in set of common Routed UI Commands out of the box which can be used:

- [ApplicationCommands](https://msdn.microsoft.com/en-us/library/system.windows.input.applicationcommands.aspx?ref=blog.jsinh.in)
- [MediaCommands](https://msdn.microsoft.com/en-us/library/system.windows.input.mediacommands.aspx?ref=blog.jsinh.in)
- [ComponentCommands](https://msdn.microsoft.com/en-us/library/system.windows.input.componentcommands.aspx?ref=blog.jsinh.in)
- [NavigationCommands](https://msdn.microsoft.com/en-us/library/system.windows.input.navigationcommands.aspx?ref=blog.jsinh.in)
- [EditingCommands](https://msdn.microsoft.com/en-us/library/system.windows.documents.editingcommands.aspx?ref=blog.jsinh.in)

These commands can be executed by any UI control that inherits from [ICommandSource](https://msdn.microsoft.com/en-us/library/system.windows.input.icommandsource.aspx?ref=blog.jsinh.in) interface which attaches the capabilities on how to invoke a command to that UI element object.

Custom implementation can be done using [CommandBinding](https://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.aspx?ref=blog.jsinh.in) class on custom UI controls. As soon as the command is triggered, it tries to find CommandTarget and bubbles up the visual tree to find the control that have command binding for that command and execute the same if `CanExecute` is true.

Here we demonstrate a simple example of `ApplicationCommands` \- Cut, Copy and Paste commands using Menu control and two text box.

![WPF command sample](https://blob.jsinh.in/media/2012/03/WPF_Command_SS01.png)

In this example when we type something in the text box one, cut and copy menu items gets enabled. You can either cut or copy text using those menu options and click on text two text box and click paste menu item.

Code for above sample:

```
<Window x:Class="WPFExamples.CommandGlance"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CommandGlance" Height="165" Width="265">
    <Grid>
        <Menu Margin="0,0,0,101">
            <MenuItem Header="Cut" Command="Cut"></MenuItem>
            <MenuItem Header="Copy" Command="Copy"></MenuItem>
            <MenuItem Header="Paste" Command="Paste"></MenuItem>
        </Menu>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="111,62,0,0" Name="txtBoxOne" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="111,91,0,0" Name="txtBoxTwo" VerticalAlignment="Top" Width="120" />
        <Label Content="Demo Text One" Height="28" HorizontalAlignment="Left" Margin="12,60,0,0" Name="lblTextBoxOne" VerticalAlignment="Top" />
        <Label Content="Demo Text Two" Height="28" HorizontalAlignment="Left" Margin="12,89,0,0" Name="lblTextBoxTwo" VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,31,0,0" Name="textBlockTitle" Text="WPF Command Example" VerticalAlignment="Top" />
    </Grid>
</Window>

```

This shows how easily WPF built-in commands can be used with out any efforts right in the XAML and no C# code. Here `CanExecute` returns `true` when text is highlighted and `Execute` will cut / copy the selected text to your clipboard.

Happy coding !!