按钮式内容的访问属性

时间:2015-11-24 10:31:11

标签: c# wpf data-binding controltemplate

我尝试为按钮创建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传递给模板非常有意义。

1 个答案:

答案 0 :(得分:1)

您需要设置relative source

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