WPF Button鼠标悬停背景异常,如何设置橙色背景?
问题描述
我使用的Button控件代码如下:
<Button Name="buttonAttiva" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,560,0,0"> <TextBlock Text="Attiva" /> </Button>
对应的样式定义:
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource MahApps.Styles.Button}"> <Setter Property="HorizontalAlignment" Value="Left" /> <Setter Property="VerticalAlignment" Value="Top" /> <Setter Property="Width" Value="150" /> <Setter Property="Height" Value="46" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="Background" Value="#81b3d4" /> <Setter Property="Foreground" Value="#ffffff" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontSize" Value="18" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="controls:ControlsHelper.MouseOverBorderBrush" Value="#000000" /> <Setter Property="controls:ControlsHelper.FocusBorderBrush" Value="#000000" /> <Setter Property="controls:ControlsHelper.FocusBorderThickness" Value="0" /> <Style.Resources> <SolidColorBrush x:Key="MahApps.Brushes.Gray7" Color="#000000" /> <SolidColorBrush x:Key="MahApps.Brushes.Gray8" Color="#000000" /> </Style.Resources> <Style.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="Background" Value="#000000" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="#000000" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#000000" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="#000000" /> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="#000000" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="#000000" /> </Trigger> </Style.Triggers> </Style>
Button正常显示效果:
但鼠标悬停时,会出现异常的灰色背景:
我需要设置正确的橙色悬停背景,目前使用了MahApps.Metro,推测是它的默认样式覆盖了我的设置。
解决方案
MahApps.Metro的Button样式内部使用了模板触发器(TemplateTriggers),优先级高于你定义的样式触发器(Style.Triggers),所以你的悬停背景设置不会生效,反而显示了MahApps默认的灰色过渡效果。
要解决这个问题,你可以通过以下两种方式处理:
方法一:覆盖MahApps的内置资源键
MahApps的Button模板会引用特定资源键定义状态样式,直接在样式的Resources节点中覆盖这些键值即可:
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource MahApps.Styles.Button}"> <!-- 保留原有Setter设置 --> <Setter Property="HorizontalAlignment" Value="Left" /> <Setter Property="VerticalAlignment" Value="Top" /> <Setter Property="Width" Value="150" /> <Setter Property="Height" Value="46" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="Background" Value="#81b3d4" /> <Setter Property="Foreground" Value="#ffffff" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontSize" Value="18" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="controls:ControlsHelper.MouseOverBorderBrush" Value="#000000" /> <Setter Property="controls:ControlsHelper.FocusBorderBrush" Value="#000000" /> <Setter Property="controls:ControlsHelper.FocusBorderThickness" Value="0" /> <Style.Resources> <!-- 覆盖MahApps悬停背景(设置为橙色#FFA500) --> <SolidColorBrush x:Key="MahApps.Brushes.Button.MouseOver.Background" Color="#FFA500" /> <SolidColorBrush x:Key="MahApps.Brushes.Button.MouseOver.Border" Color="#000000" /> <!-- 保留原有Gray7/Gray8覆盖 --> <SolidColorBrush x:Key="MahApps.Brushes.Gray7" Color="#000000" /> <SolidColorBrush x:Key="MahApps.Brushes.Gray8" Color="#000000" /> </Style.Resources> <!-- 同步更新样式触发器的悬停背景,确保一致性 --> <Style.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="Background" Value="#000000" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="#000000" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#FFA500" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="#000000" /> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="#000000" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="#000000" /> </Trigger> </Style.Triggers> </Style>
方法二:完全自定义Button模板
如果需要更彻底的控制,可以右键Button选择「编辑模板」→「编辑副本」,直接修改模板内的触发器,彻底脱离MahApps默认样式的干扰:
<!-- 示例简化模板,仅保留核心状态逻辑 --> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="#FFA500" /> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="border" Property="Background" Value="#000000" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>
注意事项
- 如果使用的是
FlatButton样式,对应的资源键是MahApps.Brushes.FlatButton.MouseOver.Background,需要对应替换。 - 资源覆盖的优先级高于样式触发器,所以优先保证资源键的正确性。
内容的提问来源于stack exchange,提问作者markzzz
相关产品推荐
相关产品推荐

