35

I want to pass the current DataContext (which is an instance of a ViewModel) as a CommandParameter on a WPF Button. What would be the syntax I should use?

<Button 
  x:Name="btnMain"
  Command="infra:ApplicationCommands.MyCommand"
  CommandParameter="{Binding ???}"
 />
devdigital
  • 34,151
  • 9
  • 98
  • 120
  • Notice that the order matters here. `CommandParameter` must come before `Command` or else you'll see only `null` being passed as a parameter. – RedX Apr 21 '20 at 14:45

2 Answers2

72

An empty Binding, without a path, binds directly to the DataContext, so

{Binding}

is enough to make it work! Your example:

<Button 
  x:Name="btnMain"
  Command="infra:ApplicationCommands.MyCommand"
  CommandParameter="{Binding}"
 />
Arcturus
  • 26,677
  • 10
  • 92
  • 107
13
<Button 
   x:Name="btnMain"
   Command="infra:ApplicationCommands.MyCommand"
   CommandParameter="{Binding}" 
/>

as long as the button is within the Visual tree of the item with the DataContext

sidney.andrews
  • 5,146
  • 3
  • 23
  • 29