2014-02-11 24 views
1

假設我有以下幾點:如何獲得一個控件模板內的目標的屬性值

<ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type Button}"> 
    <Grid> 
     <Rectangle Width="100" Height="20" /> 
    </Grid> 
</ControlTemplate> 

<Button Template="MyButtonTemplate" Width="25" /> 
<Button Template="MyButtonTemplate" Width="50" /> 
<Button Template="MyButtonTemplate" Width="75" /> 

什麼我需要做的控件模板有矩形是按鈕的寬度,而不是每次硬編碼100次?有沒有辦法讓我從我的控件模板中訪問我的目標類型的屬性值?

謝謝。

回答

2

如何:

<Rectangle Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" /> 
2

完全不指定RectangleWidthHeight,讓它伸展到Button的大小:

<ControlTemplate TargetType="{x:Type Button}"> 
    <Grid> 
     <Rectangle/> 
    </Grid> 
</ControlTemplate> 
相關問題