【问题标题】:Access property of content within button-style按钮样式中内容的访问属性
【发布时间】:2016-02-26 18:01:36
【问题描述】:

我正在尝试为按钮创建一个ControlTemplate 并将CommandParameter 绑定到按钮Content 的某些属性。

目前看起来像这样:

<Style x:Key="MyStyleKey" TargetType="{x:Type Button}">
    <Setter Property="controls:ButtonHelper.CornerRadius" Value="3"/>
    // stuck here
    <Setter Property="CommandParameter" Value="{Binding ((SomeDataClass)Content).Id}" /> 
    <Setter Property="Template">
        <Setter.Value>
            // ...
        </Setter.Value>
    </Setter>
</Style>

被称为

<Button Command="{Binding SetActive}" Content="{Binding SomeDataObject}" Style="{DynamicResource MyStyleKey}" />

通常我会直接设置CommandParameter

<Button Command="{Binding SetActive}" CommandParameter="{Binding SomeDataObject.Id}" Content="{Binding SomeDataObject}" Style="{DynamicResource MyStyleKey}" />

我对模板的理解是不要重复自己。 由于Id-property 是按钮Content 的一部分,将其作为CommandParameter 传递给模板是完全有意义的。

【问题讨论】:

  • 不清楚你在问什么,至少对我来说是这样。
  • @FarhanAnam:如何访问绑定Content的按钮的属性Id-要传递给CommandParameter的属性
  • 你问题的最后两行让我很困惑。

标签: c# wpf data-binding controltemplate


【解决方案1】:

你需要设置relative source:

<Setter Property="CommandParameter" Value="{Binding Content.Id, RelativeSource={RelativeSource Mode=Self}}"/>

【讨论】:

  • 这是有效的。谢谢。输入 Content. 时是否有机会启用智能感知,或者 SO here 上描述的唯一方法是什么?
  • @KingKerosin:您提到的解决方案不适用于此绑定表达式。
  • 使用{Binding Content.(someNameSpace:SomeDataClass.Id), RelativeSource={RelativeSource Self} } 正在工作。在设计时给我智能感知,在运行时给我正确的绑定。为什么不应该呢?
  • @KingKerosin:我必须更清楚。 Intellisense 不工作,绑定没问题。我在 VS 2015 上。
  • 对不起,我的错。当然输入Content.时没有智能感知,但Content.(someNameSpace.SomeDataClass.会通过智能感知提供Id。有没有其他方法,例如将d:??? 设置为在设计时进行智能感知?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
  • 2018-02-07
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 2017-05-18
相关资源
最近更新 更多