2013-11-02 28 views

回答

0

以通用基礎樣式定義模板(例如RadioButtonBaseStyle),其中您不用硬編碼背景顏色,而是使用{TemplateBinding Background}代替;然後根據RadioButtonBaseStyle創建幾種樣式,您只需使用setter更改Background屬性。

例子:

<Style x:Key="RadioButtonBaseStyle" TargetType="RadioButton"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="RadioButton> 
       ... 
       <Ellipse Fill="{TemplateBinding Background}" /> 
       ... 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="BlueRadioButtonStyle" TargetType="RadioButton" BasedOn="{StaticResource RadioButtonBaseStyle}"> 
    <Setter Property="Background" Value="Blue" /> 
</Style> 

如果您需要更精細的控制,你可以創建額外的附加屬性,而使用他們的標準Background/Foreground/BorderBrush的,如本博客文章解釋說:http://www.thomaslevesque.com/2011/10/01/wpf-creating-parameterized-styles-with-attached-properties/