可以被其他样式继承的WPF通用样式

WPF Generic style that can be inherited by other styles

我首先说我没有太多使用 WPF 的经验,因为我刚刚开始使用它(我以前的所有 C# 经验都是使用 Windows Forms 和 ASP.net)。

假设我在 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>

我如何合并这两种样式以创建 "contains both" 的通用样式?

编辑:

Dmitriy 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>

然后自动设置两种渐变颜色?

我刚刚为您创建了一个简单的示例来向您展示如何做到这一点。您应该描述具有通用属性的基本样式。然后只需使用 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>

实现此目的的一种方法是定义样式,而不是在样式本身中提供渐变,您可以使用如下所示的 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>

本质上,您想要创建的是基于 'parameterized' 样式的样式。

您需要做的是使用动态资源为 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>

基于样式:

<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>

按钮堆叠面板的屏幕截图: