Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Phone 7 (WP7) Change a button's background color on click

This seems like a really, really simple problem, but I can't figure it out. The culprit appears to be WP7's default style. It changes the background color to white when a button is clicked, then back to the button's default background.

The problem I have is I want to change the button's background when the button is clicked. I can't find any possible way to do this.

I've tried setting the background in code, but that does nothing. I think it's being overwritten by the default style.

I've tried using a Property Change behavior in Blend, but that has the exact same result.

I've tried creating a new visual state for the button and setting that on click, but that's a little buggy and has a large overhead for the number of buttons I'm dealing with. Also, it didn't work.

I can set other buttons' background on a click event, just not the button being clicked.

This is such an annoying roadblock! I'm sure this is a one line of code kind of answer. :)

like image 604
Jason Avatar asked Aug 13 '10 12:08

Jason


2 Answers

What you need to do is create a button template that modifies the Pressed visual state.

In blend, select your button, click the menu item "Object"->"Edit Template"->"Edit a Copy..." and a new template is created. In the States window, select the Pressed visual state in the CommonStates visual state group. Now select ButtonBackground in the object hierarchy and edit the background brush in the Properties window.

I edited the Pressed state's background to be a solid Cyan-ish color and ended up with something like this XAML.

<phone:PhoneApplicationPage ...>     <phone:PhoneApplicationPage.Resources>         <Style x:Key="ButtonStyle1" TargetType="Button">             <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="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="Cyan" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/>                                         </Storyboard>                                     </VisualState>                                     <VisualState x:Name="Disabled">                                         <Storyboard>                                             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">                                                 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>                                             </ObjectAnimationUsingKeyFrames>                                             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">                                                 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>                                             </ObjectAnimationUsingKeyFrames>                                         </Storyboard>                                     </VisualState>                                 </VisualStateGroup>                             </VisualStateManager.VisualStateGroups>                             <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}" Background="Black">                                 <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>                             </Border>                         </Grid>                     </ControlTemplate>                 </Setter.Value>             </Setter>         </Style>     </phone:PhoneApplicationPage.Resources>      <Grid x:Name="LayoutRoot" Background="Transparent">         <Button Content="Button" Style="{StaticResource ButtonStyle1}"/>     </Grid> </phone:PhoneApplicationPage> 
like image 92
Matt Casto Avatar answered Oct 22 '22 09:10

Matt Casto


I think getting a reference to the actual background, then changing that might help. Here's a method that will take an instance a button.

        private void HighlightButton(Button btnToHighlight)         {              SolidColorBrush sBrush = (SolidColorBrush)btnToHighlight.Background;               sBrush.Color = //enter your colour here             btnToHighlight.Background = sBrush;          } 
like image 40
keyboardP Avatar answered Oct 22 '22 09:10

keyboardP