为什么在触发MouseOver状态后,ColorAnimation for Checked状态不能保持颜色不变? [英] Why is the ColorAnimation for Checked state not persisting color after the MouseOver state is triggered?

查看:101
本文介绍了为什么在触发MouseOver状态后,ColorAnimation for Checked状态不能保持颜色不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的ToggleButtonControlTemplate遇到问题.

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.

我的ControlTemplateVisualStateManager部分看起来像这样:

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>

推荐答案

我所涉及的问题的解决方法是创建一个包含BorderGrid.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆