视觉状态–返回正常状态

时间:2018-08-28 11:18:49

标签: wpf controltemplate visualstatemanager visualstates

在以下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的解决方案感到好奇。

0 个答案:

没有答案