C# VisualStates切换按钮工作不正常

C# VisualStates切换按钮工作不正常,c#,windows-runtime,windows-store-apps,winrt-xaml,C#,Windows Runtime,Windows Store Apps,Winrt Xaml,我使用VisualStateManager为切换按钮设置了默认样式 <Style TargetType="ToggleButton"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ToggleButton"> <Grid Width="{TemplateBindi

我使用VisualStateManager为切换按钮设置了默认样式

<Style TargetType="ToggleButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard Duration="0" RepeatBehavior="Forever">
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundEllipse" Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PageBackgroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonTextColor}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PointerOver"/>
                            <VisualState x:Name="Pressed"/>
                            <VisualState x:Name="Disabled"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Checked">
                                <Storyboard Duration="0" RepeatBehavior="Forever">
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundEllipse" Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonCheckedThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PageBackgroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unchecked"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Ellipse x:Name="BackgroundEllipse" Stroke="White" StrokeThickness="3"/>
                    <ContentPresenter x:Name="ContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

不幸的是,更改只工作一次,并且在检查togglebutton时不会触发状态更改为checked,而是在鼠标选中按钮后离开按钮时触发

我已经尝试过在“正常”状态下使用相同的故事板,也在“未检查”状态下使用。 早些时候,我还使用了“指针覆盖”和“按下”状态,但它们完全搞砸了,因为最终它保持在“按下”状态而不是“选中”,有时它会进行完全随机的更改,比如只应用一半的样式

所以我的问题是:我做错了什么?我如何正确使用VisualStates


Bonusquestion:我如何告诉“PointerOver”状态,只有在CheckState为“Unchecked”时才应应用它?

侧注:“duration=“0”和“RepeatBehavior=“Forever”属性只是在测试。将这些内容从
正常的
中拉出来,直接应用于对象。作为参考,将你正在做的事情与实际情况进行比较,你可能会很快注意到你的愚蠢行为,如果没有,我们可以回来,我会带你去看。Cheers@Stefan你能补充一下答案吗?我不确定。已经有一段时间了。不再是那个努比了。但我想我只是照Chris说的做了,从正常状态删除了内容,并将其直接应用到元素,因为在默认状态下应用转换有点愚蠢,因为它是默认状态。我可以检查我是否还有那个代码,但是如果你为了得到答案而想要答案的话。