为什么在触发MouseOver状态后,ColorAnimation for Checked状态不能保持颜色不变? [英] Why is the ColorAnimation for Checked state not persisting color after the MouseOver state is triggered?
问题描述
我创建的ToggleButton
的ControlTemplate
遇到问题.
I encountered an issue with a ControlTemplate
for ToggleButton
I created.
当按钮为Checked
时,将触发ColorAnimation
,并且控件的背景会更改颜色.但是,如果用户进入MouseOver
状态,则会触发另一个动画,该动画也会影响按钮的背景.
When the button is Checked
, a ColorAnimation
is triggered and the control's background changes color. However, if the user enters the MouseOver
state, another animation is triggered that affects the button's background as well.
当鼠标不再处于MouseOver
状态时,控件不会恢复为处于Checked
状态时应具有的颜色.我不确定为什么MouseOver
状态被触发时,这种情况不会持续.
When the mouse is no longer in the MouseOver
state, the control does not return to the color it should be while it is in the Checked
state. I'm not sure why this does not persist when the MouseOver
state is triggered.
我的ControlTemplate
的VisualStateManager
部分看起来像这样:
The VisualStateManager
portion of my ControlTemplate
looks sorta like this:
<VisualStateManger.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"></VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="BackgroundBorder"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="Gold" Duration="0:0:0.3" />
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="CheckedStates">
<VisualState x:Name="Checked">
<Storyboard>
<ColorAnimation Storyboard.TargetName="BackgroundBorder"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="PaleGoldenrod" Duration="0:0:0.3" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
推荐答案
我所涉及的问题的解决方法是创建一个包含Border
的Grid
.
My workaround for the issue I was having involved creating a Grid
that enclosed the Border
.
对于CommonStates
,我将动画更改为Border.Background
,对于CheckedStates
,我将动画更改为Grid.Background
.
For the CommonStates
I made animation changes to the Border.Background
and for the CheckedStates
I made animation changes to the Grid.Background
.
它可以达到我想要的视觉效果.
It achieves the visual effect I was looking for.
这篇关于为什么在触发MouseOver状态后,ColorAnimation for Checked状态不能保持颜色不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!