WP7 - 单击按钮后无法设置按钮的背景

时间:2012-04-20 14:46:52

标签: windows-phone-7 xaml

我正在使用这个记忆游戏应用程序,试图学习WP7。想象一下屏幕上有6个独特的彩色按钮,用户必须重复这个按钮。

  1. 首先通过,禁用所有按钮,更改Button1的背景
  2. 启用所有按钮,用户点击按钮1,游戏继续......
  3. 第二次通过,禁用所有按钮,按钮1和3将突出显示
  4. 只更改Button3的背景(调试显示更改Button1背景的程序已运行)
  5. 按钮的突出显示只是变为较浅的背景。无论点击什么按钮,都无法更改次要传递中的背景颜色。

    样式XAML:

    <phone:PhoneApplicationPage.Resources>
            <Style x:Key="ButtonStyle1" TargetType="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Grid Background="Transparent">
                                <VisualStateManager.VisualStateGroups>
                                    <!--Define the states for the common states. The states in a 
                                            VisualStateGroup are mutually exclusive to each other.-->
                                    <VisualStateGroup x:Name="CommonStates">
                                        <!--Define the VisualStates in this VistualStateGroup.-->
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="MouseOver" />
                                        <VisualState x:Name="Pressed">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                                <ColorAnimation Duration="0" To="White" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/>
                                            </Storyboard>                                        
                                        </VisualState>
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <DoubleAnimation Duration="0" Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Opacity" To="1"/>
                                            </Storyboard>
                                        </VisualState>
                                        </VisualStateGroup>
                                    <!--Define the states for the focus states. The states in a 
                                            VisualStateGroup are mutually exclusive to each other.-->
                                    <VisualStateGroup x:Name="FocusStates">
                                        <!--Define the VisualStates in this VistualStateGroup.-->
                                        <VisualState x:Name="Focused" />
                                        <VisualState x:Name="Unfocused" />
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}" Background="{TemplateBinding Background}">
                                    <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                </Border>
                            </Grid>
                            <!--The parts of the button control will be defined here.-->
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </phone:PhoneApplicationPage.Resources>
    

    按钮:

    <Button Height="121" HorizontalAlignment="Left" Margin="14,61,0,0" Name="but1" VerticalAlignment="Top" Width="197" Background="#FF7D0000" BorderBrush="Black" Foreground="Red" BorderThickness="0" Padding="0" Style="{StaticResource ButtonStyle1}" Click="but1_Click" />
    

    我应该复制一个StackPanel / Rectangle来替换我的按钮吗?或者我缺少一种风格或基本的东西?

1 个答案:

答案 0 :(得分:0)

解决。

我改变了按钮风格的按下状态 - 这是在比较了我的风格和按钮的默认风格之后。我似乎错过了一些代码。

<VisualState x:Name="Pressed">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

这是在黑暗中刺伤:)