更改鼠标悬停时的按钮字体权重和/或字体样式



我想创建一个按钮,在鼠标悬停时动态更改其样式。我要更改"前景"、"背景"、"边框"、"字体权重"和/或"字体样式"。我的窗口中有以下XAML。资源…

<Window.Resources>
<Style TargetType="Button" x:Key="mouseOverStyle">
<Setter Property="Foreground" Value="#FF808080" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="TextBlock.FontWeight" Value="Normal" />
<Setter Property="TextBlock.FontStyle" Value="Normal" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button" >
<Border x:Name="Border" Background="{TemplateBinding Background}"  BorderBrush="Transparent" BorderThickness="2" >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers >
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="#FF303030" />
<Setter Property="TextBlock.FontWeight" Value="Bold" />
<Setter Property="TextBlock.FontStyle" Value="Italic" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>

在鼠标悬停时,"背景"、"前景"one_answers"边界"属性将正确更改。但是,字体权重和字体样式不变。

请告诉我我缺少了什么,或者我需要改变什么。谢谢

给定定义样式的方式,您需要明确告诉按钮要使用哪种样式,如下所示:

<Button Style="{StaticResource mouseOverStyle}" />

您可以使用一些替代方案来隐式地告诉应用程序中的所有控件应用样式。让我们知道,答案可以扩展到包括这种方法作为替代方案。

最新更新