[英]Access property of content within button-style
我正在嘗試為按鈕創建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
傳遞給模板是完全有意義的。
您需要設置相對源 :
<Setter Property="CommandParameter" Value="{Binding Content.Id, RelativeSource={RelativeSource Mode=Self}}"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.