VisualStates ToggleButton无法正常工作

时间:2014-06-19 16:44:51

标签: c# windows-runtime windows-store-apps winrt-xaml

我使用VisualStateManager

为我的ToggleButtons设置了默认样式
<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时未触发检查状态更改,但是在检查完鼠标后鼠标离开按钮时。

我尝试使用我在&#34;普通&#34;中使用的相同故事板。也适用于&#34;未选中&#34;州。 之前我还使用过&#34; PointerOver&#34;和#34;按下&#34;但他们完全搞砸了,因为最终它留在了#34; Pressed&#34;国家而不是&#34; Checked&#34;有时它完全是随机的变化,比如只应用一半的风格。

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

Bonusquestion:我如何告诉&#34; PointerOver&#34;声明只有在CheckState为&#34;未选中&#34;?

时才应用

0 个答案:

没有答案