簡體   English   中英

按鈕樣式內的內容訪問屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM