1
我有五個單選按鈕,它們都具有相同的風格但顏色不同。樣式都是分開的。我想做一個主要的風格,並能夠分別改變他們每個人的背景顏色。我該如何設計一個單選按鈕作爲主要設計並更改每個按鈕的主要設計
我有五個單選按鈕,它們都具有相同的風格但顏色不同。樣式都是分開的。我想做一個主要的風格,並能夠分別改變他們每個人的背景顏色。我該如何設計一個單選按鈕作爲主要設計並更改每個按鈕的主要設計
以通用基礎樣式定義模板(例如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/