在以下Button
样式中,我通过VisualStateManager
更改了 MouseOver 视觉表示。由于整个ObjectAnimationUsingKeyFrames
的变化,我更喜欢使用Brush
。
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.Background)">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{x:Static Brushes.Red}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.Foreground)">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{x:Static Brushes.White}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当我进入 MouseOver 状态时,背景和前景都会被更改。当我将鼠标移出时,只有背景变回了 Normal 状态。为什么?
注意:我通过Trigger
中的Style
知道了解决方案。我对通过VisualStateManager
的解决方案感到好奇。