<Window.Resources>
<!-- 定义样式 -->
<Style x:Key="CustomButtonStyle" TargetType="Button">
<Setter Property="Tag" Value="{x:Static local:ButtonColorSets.Button1Colors}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<!-- 正常状态 -->
<Trigger Property="IsPressed" Value="False">
<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.NormalColor}"/>
<Setter TargetName="border" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.NormalColor}"/>
</Trigger>
<!-- 按下状态 -->
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.PressedColor}"/>
<Setter TargetName="border" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.PressedColor}"/>
</Trigger>
<!-- 鼠标悬停状态 -->
<Trigger Property="IsMouseOver" Value="True">
<!--<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.MouseOverColor}"/>-->
</Trigger>
<!-- 禁用状态 -->
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.DisabledColor}"/>
<Setter TargetName="border" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.DisabledColor}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- 设置默认 Tag -->
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Value="{x:Null}">
<Setter Property="Tag" Value="{x:Static local:ButtonColorSets.Button1Colors}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel Background="#fff">
<!-- 不设置 Tag 的按钮,将使用默认颜色 -->
<Button Content="Button 1" Width="100" Height="30" Style="{StaticResource CustomButtonStyle}" HorizontalAlignment="Left" />
<!-- 设置 Tag 为第二个颜色 -->
<Button Content="Button 2" Width="100" Height="30" Style="{StaticResource CustomButtonStyle}"
Tag="{x:Static local:ButtonColorSets.Button2Colors}" HorizontalAlignment="Left" />
<Button IsEnabled="True" Content="Button 3" Width="100" Height="30" Style="{StaticResource CustomButtonStyle}"
Tag="{x:Static local:ButtonColorSets.Button3Colors}" HorizontalAlignment="Left" />
</StackPanel>
public class ButtonColors
{
public Brush NormalColor { get; set; }
public Brush PressedColor { get; set; }
public Brush MouseOverColor { get; set; }
public Brush DisabledColor { get; set; }
}
public static class ButtonColorSets
{
public static ButtonColors Button1Colors { get; } = new ButtonColors
{
NormalColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#111")),
PressedColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#222")),
MouseOverColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#333")),
DisabledColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#444"))
};
public static ButtonColors Button2Colors { get; } = new ButtonColors
{
NormalColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#555")),
PressedColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#666")),
MouseOverColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#777")),
DisabledColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#888"))
};
public static ButtonColors Button3Colors { get; } = new ButtonColors
{
NormalColor = Brushes.Red,
PressedColor = Brushes.Pink,
MouseOverColor = Brushes.SkyBlue,
DisabledColor = Brushes.Green
};
}