2015-11-24 28 views
1

我正在尝试为按钮创建一个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开始 - 属性是按钮Content的一部分,将它作为CommandParameter传递给模板是完全有意义的。

+0

目前还不清楚你问什么,至少对我来说。 –

+0

@FarhanAnam:如何访问属性'Id'的按钮绑定'Content'属性被传递给'CommandParameter' – KingKerosin

+0

你问题的最后两行让我困惑。 –

回答

1

您需要设置relative source

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

这是行得通的。谢谢。 输入“Content.”或任何在SO [here](http://stackoverflow.com/questions/16560550/wpf-binding-casting-in-binding-path)上描述的内容时,任何可能启用智能感知的机会办法? – KingKerosin

+0

@KingKerosin:您提到的解决方案不适用于此绑定表达式。 – Dennis

+0

使用'{Binding Content。(someNameSpace:SomeDataClass.Id),RelativeSource = {RelativeSource Self}}'正在工作。在设计时给予我intellisense,并在运行时正确绑定。为什么不呢? – KingKerosin