37

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 ???}"
 />
2
  • 1
    Notice that the order matters here. CommandParameter must come before Command or else you'll see only null being passed as a parameter. Commented Apr 21, 2020 at 14:45
  • Nope, I just tested it; the order does not matter! Commented Dec 17, 2023 at 18:36

2 Answers 2

75

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}"
 />
Sign up to request clarification or add additional context in comments.

1 Comment

This is so simple it is brilliant. Nice :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.