Windows Phone 8.1 button color on tap

ぃ、小莉子 提交于 2019-12-11 13:16:58

问题


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?


回答1:


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>

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

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.



来源:https://stackoverflow.com/questions/25064126/windows-phone-8-1-button-color-on-tap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!