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

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

问题描述

我有一个DataTemplate的重新presents我通过自定义AppBarCommand对象的集合。

宣布AppBar按钮

 公共AppBarCommand(RelayCommand命令字符串的ButtonStyle)
  {
     命令=命令;
     的ButtonStyle =的ButtonStyle;
  }<&DataTemplate的GT;
   <按钮命令={绑定命令}
           风格={结合的ButtonStyle,转换器= {StaticResource的StringNameToStyleConverter}}/>
< / DataTemplate中>

我想补充一个CommandParameter约束力,但该参数必须是按钮本身。这是这样我就可以设置一个木卫四弹出的PlacementTarget。这可能吗?


解决方案

 <按钮命令={绑定命令}
        CommandParameter ={绑定的RelativeSource = {自我的RelativeSource}}/>

您Command属性应该是 RelayCommand 通用版: RelayCommand<对象> 例如

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>

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

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

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

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