我尝试为按钮创建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 :(得分:1)
您需要设置relative source:
<Setter Property="CommandParameter" Value="{Binding Content.Id, RelativeSource={RelativeSource Mode=Self}}"/>