Button的按下VisualState的动画不会触发

时间:2013-03-11 13:46:38

标签: windows xaml windows-phone-8 windows-phone visualstatemanager

我正在开发一个Windows Phone 8项目。我对这个按钮的问题是:

        <Button x:Name="decreaseFontButton" Foreground="{StaticResource GlobalBrush}">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Pressed">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Height" Storyboard.TargetName="decreaseFontButton">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="70"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="decreaseFontButton" Completed="ObjectAnimationUsingKeyFrames_Completed_1">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource GlobalBrush}"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Button.Content>
                <Image Source="/Assets/Player/sub_minus.png" Width="28" Height="28"/>
            </Button.Content>
        </Button>

是两个动画中没有一个被触发。我试图将VisualStateGroup的名称更改为 FocusState ,将VisualState的名称更改为聚焦,但我也没有看到任何更改。你能告诉我我做错了吗?

感谢您的关注!

1 个答案:

答案 0 :(得分:1)

问题在于您是如何尝试修改Visual状态的。这些实际上是按钮内部模板的一部分,不能按照您尝试的方式进行配置。

相反,您应该创建一个新样式,其中包含您想要的视觉状态更改,并将该样式应用于按钮。

像这样:

<phone:PhoneApplicationPage.Resources>

    <SolidColorBrush Color="Aqua" x:Key="aquabrush" />

    <Style x:Key="AlternativeButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
        <Setter Property="Padding" Value="10,5,10,6"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource aquabrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Height" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="70"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled" />
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates"/>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Button Content="pressed?" Style="{StaticResource AlternativeButtonStyle}" />
</Grid>