The Basic Command Binding
Any control implementing ICommandSource exposes a Command dependency property that you bind to a ViewModel's ICommand property using standard binding syntax, '{Binding SaveCommand}', which by default resolves against the control's DataContext, meaning the binding walks up the visual tree until it finds a DataContext (usually the ViewModel set on the Window or UserControl) exposing a SaveCommand property. Because this is an ordinary binding, all the usual binding features apply: you can bind Command inside a DataTemplate, a Style, or even a ControlTemplate, and the resolved ICommand instance is cached and reused across CanExecute re-evaluations rather than re-created on every check.
Cricket analogy: It's like a stadium's PA announcer resolving 'the current bowler' by looking at the scoreboard's active DataContext rather than hardcoding a name, so the same announcement template works correctly whether Bumrah or Shami is bowling.
<Window x:Class="MyApp.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<StackPanel>
<TextBox Text="{Binding SearchTerm, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="Search"
Command="{Binding SearchCommand}"
CommandParameter="{Binding SearchTerm}"
IsDefault="True"/>
<Button Content="Cancel" Command="{Binding CancelCommand}" IsCancel="True"/>
</StackPanel>
</Window>Invoking Commands Beyond Buttons
Commands aren't limited to Button clicks: InputBindings let you invoke a command from a keyboard shortcut via '<Window.InputBindings><KeyBinding Key="S" Modifiers="Control" Command="{Binding SaveCommand}"/></Window.InputBindings>', and MouseBinding does the same for mouse gestures. For non-ICommandSource events, like a ListBox's SelectionChanged or a control's MouseEnter, you either use Microsoft.Xaml.Behaviors' InvokeCommandAction inside an EventTrigger to route arbitrary events to a command, or, in stricter MVVM setups, expose the interaction as a proper bindable property instead of forcing every event through a command.
Cricket analogy: It's like how a captain can call for a review not just via the on-field hand signal (Button-equivalent) but also via a designated radio channel to the dressing room (KeyBinding), two different triggers invoking the same underlying DRS review action.
CommandTarget and RoutedCommand
RoutedCommand, the older WPF-native command type used by ApplicationCommands.Copy and similar built-ins, differs from a ViewModel-owned ICommand in that it relies on routed events and CommandBindings placed on a specific element, with CommandTarget determining which element's command bindings handle the request; when CommandTarget is left unset, it defaults to the currently focused element. In modern ViewModel-first MVVM, RoutedCommand is largely superseded by RelayCommand-style bound commands because CommandTarget's focus-dependent routing is harder to reason about than a direct ViewModel property binding, though RoutedCommand still shows up for editing commands like Copy, Paste, and Undo that integrate with built-in TextBox behavior.
Cricket analogy: It's like the difference between an umpire who reacts to whichever player currently has the ball in hand (focus-dependent RoutedCommand routing) versus a fixed designated reviewer role, say the third umpire, who always handles reviews regardless of who's holding the ball (a direct ViewModel-bound command).
You can bind Command inside a Style's Setter (via a DataTrigger or directly) to give every instance of a custom control the same command binding without repeating it per usage, useful for things like a consistent 'context menu delete' action applied to many ListBoxItem styles.
InvokeCommandAction from Microsoft.Xaml.Behaviors requires adding the Microsoft.Xaml.Behaviors.Wpf NuGet package; forgetting the xmlns:i="http://schemas.microsoft.com/xaml/behaviors" namespace declaration is a frequent cause of XAML parse errors when wiring event-to-command behaviors.
- Command dependency properties on ICommandSource controls bind directly to ViewModel ICommand properties.
- Standard binding resolution walks the DataContext, so Command bindings work inside DataTemplates and Styles.
- KeyBinding and MouseBinding invoke commands from keyboard shortcuts and mouse gestures respectively.
- InvokeCommandAction from Microsoft.Xaml.Behaviors routes arbitrary events, like SelectionChanged, to a command.
- RoutedCommand is the older WPF-native command type, relying on CommandTarget and focus-based routing.
- CommandTarget defaults to the focused element when left unset on a RoutedCommand invocation.
- Modern ViewModel-first MVVM favors direct RelayCommand bindings over RoutedCommand's focus-dependent model.
Practice what you learned
1. What determines which DataContext a Button's '{Binding SaveCommand}' resolves against?
2. Which XAML element invokes a command in response to a keyboard shortcut like Ctrl+S?
3. What does InvokeCommandAction from Microsoft.Xaml.Behaviors enable?
4. What does CommandTarget default to when left unset on a RoutedCommand invocation?
5. Why do most modern ViewModel-first MVVM codebases prefer RelayCommand-bound ICommand over RoutedCommand?
Was this page helpful?
You May Also Like
The ICommand Interface
The core .NET interface that lets ViewModels expose actions to the View without any code-behind, forming the backbone of command-based interaction in MVVM.
Command Parameters
How CommandParameter passes contextual data from a View control into a ViewModel's Execute and CanExecute logic, and the patterns and pitfalls around its use.
The RelayCommand Pattern
A reusable ICommand implementation that wraps Execute and CanExecute delegates, eliminating the need to write a bespoke command class for every ViewModel action.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics