100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
.NET

Command Binding in XAML

The XAML-side mechanics of wiring controls to ViewModel commands, covering Command, CommandParameter, CommandTarget, KeyBinding, and InputBinding-based invocation.

CommandsIntermediate9 min readJul 10, 2026
Analogies

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.

xml
<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

Was this page helpful?

Topics covered

#NET#MVVMDesignPatternStudyNotes#MicrosoftTechnologies#CommandBindingInXAML#Command#Binding#XAML#Invoking#StudyNotes#SkillVeris