我開始說這個問題的原因是我沒有太多使用WPF的經驗,因爲我剛剛開始使用它(我之前的所有C#經驗都是使用Windows Forms和ASP.net )。WPF可以被其他樣式繼承的泛型
比方說,我在我的App.xaml,一個定義了一個藍色按鈕和一個定義了紅色按鈕定義的兩種風格:
<Style x:Key="BlueButton" TargetType="Button">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF50D0FF"/>
<GradientStop Color="#FF0092C8" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="2" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF0092C8"/>
<GradientStop Color="#FF50D0FF" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="RedButton" TargetType="Button">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFFAE00" Offset="0"/>
<GradientStop Color="Red" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="2" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Red" Offset="0"/>
<GradientStop Color="#FFFFAE00" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
我怎麼能合併這兩種風格做一般性「包含兩者」的風格?
編輯:
德米特里Polyanskiy的答案的作品,但我還是要我想創建一個新的風格,每次設置的每種屬性。有沒有辦法做這樣的事情:<Style x:Key="RedButton" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}" Color1="#FFFFAE00" Color2="Red" />
或
<Style x:Key="RedButton" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}">
<Setter Property="Color1" Value="#FFFFAE00" />
<Setter Property="Color2" Value="Red" />
</Style>
然後自動設定的兩個漸變顏色?
你的意思是通過合併兩個風格?你怎麼能有一個藍色和紅色的按鈕? – Nitin
我的意思是像一般風格的東西,所以如果我想添加一個綠色的按鈕,我可以做到這一點,而無需再次寫一切。 – user3807877
我認爲您正在尋找['BasedOn'](https://msdn.microsoft.com/en-us/library/system.windows.style.basedon%28v=vs.110%29.aspx) – Default