Why is the ColorAnimation for Checked state not persisting color after the MouseOver state is triggered?

浪尽此生 提交于 2019-12-07 04:50:20

问题


I encountered an issue with a ControlTemplate for ToggleButton I created.

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.

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.

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>

回答1:


My workaround for the issue I was having involved creating a Grid that enclosed the Border.

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.



来源:https://stackoverflow.com/questions/12589636/why-is-the-coloranimation-for-checked-state-not-persisting-color-after-the-mouse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!