C# 可由其他样式继承的WPF通用样式
我在开始回答这个问题时说,我没有太多使用WPF的经验,因为我刚刚开始使用它(我以前所有的C#经验都是使用Windows窗体和ASP.net) 假设我在App.xaml中定义了两种样式,一种定义了蓝色按钮,另一种定义了红色按钮:C# 可由其他样式继承的WPF通用样式,c#,wpf,wpf-style,C#,Wpf,Wpf Style,我在开始回答这个问题时说,我没有太多使用WPF的经验,因为我刚刚开始使用它(我以前所有的C#经验都是使用Windows窗体和ASP.net) 假设我在App.xaml中定义了两种样式,一种定义了蓝色按钮,另一种定义了红色按钮: <Style x:Key="BlueButton" TargetType="Button"> <Setter Property="Foreground" Value="White" /> <Setter Property="B
<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>
如何合并这两种样式以生成“同时包含这两种样式”的常规样式
编辑:
Dmitriy Polyanskiy的答案是可行的,但每次我想要创建新样式时,我仍然必须设置每个属性。有没有这样的方法:
或
然后自动设置两种渐变颜色?我刚刚创建了一个快速示例,向您展示如何实现这一点。您应该使用公共属性描述基本样式。然后只需使用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>
我刚刚为您创建了一个快速示例,向您展示如何做到这一点。您应该使用公共属性描述基本样式。然后只需使用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>
实现这一点的一种方法是定义样式,而不是在样式本身中提供渐变,您可以使用DynamicSource,如下所示。然后,您可以为每个按钮定义它将要使用的本地资源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>
实现这一点的一种方法是定义样式,而不是在样式本身中提供渐变,您可以使用DynamicSource,如下所示。然后,您可以为每个按钮定义它将要使用的本地资源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>
本质上,您希望创建基于“参数化”样式的are样式 您需要做的是使用GradientStop颜色的DynamicResources创建基本样式。然后,在基于它的样式中,覆盖资源颜色 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>
白色
灰色
BasedOn样式:
<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>
#FFFFAE00
红色
#FF50D0FF
#FF0092C8
绿色
淡绿色
紫色
黄色的
按钮堆叠面板的屏幕截图:
本质上,您希望创建基于“参数化”样式的are样式 您需要做的是使用GradientStop颜色的DynamicResources创建基本样式。然后,在基于它的样式中,覆盖资源颜色