将 WPF 按钮 CommandParameter 绑定到 DataTemplate 中的按钮本身 [英] Binding a WPF Button CommandParameter to the Button itself in DataTemplate

查看:42
本文介绍了将 WPF 按钮 CommandParameter 绑定到 DataTemplate 中的按钮本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataTemplate 代表我通过自定义 AppBarCommand 对象集合声明的 AppBar 按钮.

I have a DataTemplate that represents AppBar buttons that I declare through a collection of custom AppBarCommand objects.

  public AppBarCommand(RelayCommand command, string buttonstyle)
  {
     Command = command;
     ButtonStyle = buttonstyle;
  }

<DataTemplate>
   <Button Command="{Binding Command}"
           Style="{Binding ButtonStyle, Converter={StaticResource StringNameToStyleConverter}}"/>
</DataTemplate>

我想添加一个 CommandParameter 绑定,但参数必须是 Button 本身.这样我就可以设置 Callisto 弹出按钮的 PlacementTarget.这可能吗?

I would like to add a CommandParameter binding, but the parameter has to be the Button itself. This is so I can set the PlacementTarget of a Callisto flyout. Is this possible?

推荐答案

<Button Command="{Binding Command}" 
        CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />

您的 Command 属性应该是 RelayCommand 的通用版本:例如 RelayCommand.

Your Command property should be the generic version of the RelayCommand: RelayCommand<object> for instance.

这篇关于将 WPF 按钮 CommandParameter 绑定到 DataTemplate 中的按钮本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆