2016-03-23 88 views
0

我開始說這個問題的原因是我沒有太多使用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> 

然後自動設定的兩個漸變顏色?

+0

你的意思是通過合併兩個風格?你怎麼能有一個藍色和紅色的按鈕? – Nitin

+0

我的意思是像一般風格的東西,所以如果我想添加一個綠色的按鈕,我可以做到這一點,而無需再次寫一切。 – user3807877

+0

我認爲您正在尋找['BasedOn'](https://msdn.microsoft.com/en-us/library/system.windows.style.basedon%28v=vs.110%29.aspx) – Default

回答

2

基本上,你想創建基於'參數化'風格的樣式。

您需要做的是使用DynamicResources爲GradientStop的顏色創建基本樣式。然後,在基於它的樣式中,覆蓋資源顏色。

BaseButtonStyle:

<Style x:Key="BaseButtonStyle" TargetType="Button"> 
    <Style.Resources> 
     <Color x:Key="Color1">White</Color> 
     <Color x:Key="Color2">Gray</Color> 
    </Style.Resources> 
    <Setter Property="Foreground" Value="White" /> 
    <Setter Property="Background"> 
     <Setter.Value> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="{DynamicResource Color1}"/> 
       <GradientStop Color="{DynamicResource Color2}" 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="{DynamicResource Color2}" /> 
         <GradientStop Color="{DynamicResource Color1}" Offset="1" /> 
        </LinearGradientBrush> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

支持算法FMP樣式:

<Style x:Key="RedButton" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> 
    <Style.Resources> 
     <Color x:Key="Color1">#FFFFAE00</Color> 
     <Color x:Key="Color2">Red</Color> 
    </Style.Resources> 
</Style> 
<Style x:Key="BlueButton" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> 
    <Style.Resources> 
     <Color x:Key="Color1">#FF50D0FF</Color> 
     <Color x:Key="Color2">#FF0092C8</Color> 
    </Style.Resources> 
</Style> 
<Style x:Key="GreenButton" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> 
    <Style.Resources> 
     <Color x:Key="Color1">Green</Color> 
     <Color x:Key="Color2">LightGreen</Color> 
    </Style.Resources> 
</Style> 
<Style x:Key="PurpleYellowButton" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> 
    <Style.Resources> 
     <Color x:Key="Color1">Purple</Color> 
     <Color x:Key="Color2">Yellow</Color> 
    </Style.Resources> 
</Style> 

按鈕的StackPanel中的截圖: enter image description here

0

我剛剛創建了一個快速示例,向您展示如何做到這一點。您應該描述具有共同屬性的基礎樣式。 ,然後只用BaseOn = {StaticResource的BaseStyle}實現這一目標的

<Style x:Key="BaseButtonStyle" TargetType="Button"> 
    <Setter Property="Foreground" Value="White" /> 
    <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> 


<Style x:Key="RedButton" TargetType="Button" 
     BasedOn="{StaticResource BaseButtonStyle}"> 
    <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> 
    <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> 
0

的一種方法是定義一個風格,而不是在風格本身給漸變您可以使用DynamicResource像下面。然後對於每個按鈕,您可以定義要使用的本地資源LinearGradientBrush並在其中設置顏色。

<Window x:Class="" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style x:Key="BaseButtonStyle" TargetType="Button"> 
      <Setter Property="Foreground" Value="White" /> 
      <Setter Property="Background" Value="{DynamicResource GradientBrushNormal}"> 
      </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" Value="{DynamicResource GradientBrushPressed}"> 
        </Setter> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <StackPanel> 
     <Button Style="{StaticResource BaseButtonStyle}" Content="Blue Button"> 
      <Button.Resources> 
       <LinearGradientBrush x:Key="GradientBrushPressed" EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="#FF0092C8"/> 
        <GradientStop Color="#FF50D0FF" Offset="1"/> 
       </LinearGradientBrush> 
       <LinearGradientBrush x:Key="GradientBrushNormal" EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="#FF50D0FF"/> 
        <GradientStop Color="#FF0092C8" Offset="1"/> 
       </LinearGradientBrush> 
      </Button.Resources> 
     </Button> 
     <Button Style="{StaticResource BaseButtonStyle}" Content="Red Button"> 
      <Button.Resources> 
       <LinearGradientBrush x:Key="GradientBrushPressed" EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="Red" Offset="0"/> 
        <GradientStop Color="#FFFFAE00" Offset="1"/> 
       </LinearGradientBrush> 
       <LinearGradientBrush x:Key="GradientBrushNormal" EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="#FFFFAE00" Offset="0"/> 
        <GradientStop Color="Red" Offset="1"/> 
       </LinearGradientBrush> 
      </Button.Resources> 
     </Button> 
    </StackPanel> 
</Window>