按下时,按钮将背景变为白色

时间:2014-03-29 07:51:59

标签: button windows-phone-8

http://2.bp.blogspot.com/-bQZRfy4g4t0/TWK05e_Bx9I/AAAAAAAAAEY/nym2bRu-RjI/s1600/5.bmp

如何在按下时将包含图像的按钮更改为白色背景?

默认情况下,当我按下我的按钮时,它不显示按钮中的图像并更改为手机强调色。

这是按钮的XAML代码:

<VisualState x:Name="Pressed">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source" Storyboard.TargetName="Img">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Assets/appbar.control.play.png" />  
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Black"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="White"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

2 个答案:

答案 0 :(得分:0)

您需要做的是创建一个按钮模板,修改按下的视觉状态。

查看thisthis SO答案是否对您有帮助,两个链接都包含Pressed视觉状态代码。

答案 1 :(得分:0)

您需要将Pressed视觉状态更改为:

(在这种情况下,按下按钮时背景会变为红色)

    <VisualState x:Name="Pressed">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source" Storyboard.TargetName="Img">
              //Set you image path like this
                <DiscreteObjectKeyFrame KeyTime="0" Value="Assets/..." />                                                                                  
            </ObjectAnimationUsingKeyFrames>  
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>