【发布时间】:2021-12-20 21:40:55
【问题描述】:
我想创建一个在鼠标悬停时动态更改其样式的按钮。我想更改 Foreground、Background、Border、FontWeight 和/或 FontStyle。我的 Window.Resources 中有以下 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>
在 MouseOver 上,Background、Foreground 和 Border 属性正确更改。但是,FontWeight 和 FontStyle 不会改变。
请告诉我我缺少什么,或者我需要改变什么。谢谢。
【问题讨论】: