轻按 Windows Phone 8.1 按钮颜色 [英] Windows Phone 8.1 button color on tap

查看:22
本文介绍了轻按 Windows Phone 8.1 按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在 WP 8.1 中触摸或点击时更改按钮颜色的问题.我已经想出了如何在没有交互时更改背景,但是如果将按钮点击为透明,我还想更改按钮的背景颜色.这可能吗?

I have a question regarding of changing button color in WP 8.1 on touch down or tap. I've already figured out how to change the background when there is no interaction, but I want to also change the background color of the button if it is tapped to transparent. Is this possible?

推荐答案

您必须修改控件模板,因为按下"状态背景被硬编码到控件中作为PhoneForegroundBrush":

You'll have to modify the control template, as the "Pressed" state background is hard-coded into the control as "PhoneForegroundBrush":

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

因此复制粘贴整个默认的ButtonBase"控件模板来自 SDK,并根据需要修改上述部分.

So copy-paste the entire default "ButtonBase" control template from the SDK, and modify the above parts as needed.

一方面,如果您希望在整个应用中使用一致的按下颜色,那么这很容易——只需更改硬编码颜色即可.另一方面,如果您想为不同的按钮使用不同的颜色,那就有点棘手了——您必须子类化 Button,并向控件添加一个新的PressedBrush"依赖属性,然后将该属性集成到控件模板中.

On the one hand, if you want a consistent pressed color throughout your app, then it's easy -- just change the hard-coded color. If on the other hand you want a different color for different buttons, then it's a bit trickier -- you'll have to subclass Button, and add a new "PressedBrush" dependency property to the control, then integrate that property into the control template.

这篇关于轻按 Windows Phone 8.1 按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆