开发者

Use DataContext as CommandParameter in WPF

开发者 https://www.devze.com 2022-12-20 20:40 出处:网络
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?

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="btnMai开发者_运维百科n"
  Command="infra:ApplicationCommands.MyCommand"
  CommandParameter="{Binding ???}"
 />


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}"
 />


<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

0

精彩评论

暂无评论...
验证码 换一张
取 消