background color change in stackpanel

windows-phone-7, wpf

Solution

You can do that by applying `Storyboard` on Click event trigger:

<ControlTemplate TargetType="HyperlinkButton">
   <StackPanel Orientation="Horizontal"  Background="#EBEBEB" x:Name="sp1">
      <Image Source="/Assets/Menu/wheretostay.png"  Stretch="None"/>
      <TextBlock />
   </StackPanel>
   <ControlTemplate.Triggers>
     <EventTrigger RoutedEvent="ButtonBase.Click">
        <BeginStoryboard>
          <Storyboard>
            <ColorAnimation To="Green" Storyboard.TargetName="sp1" 
                            Storyboard.TargetProperty="Background.Color"/>
          </Storyboard>
        </BeginStoryboard>
     </EventTrigger>
   </ControlTemplate.Triggers>
 </ControlTemplate>

For Windows Phone 7, use `Visual State`:

<ControlTemplate TargetType="HyperlinkButton">
   <ControlTemplate.Resources>
     <SolidColorBrush x:Key="PhoneBackgrounBrush" Color="Green"/>
   </ControlTemplate.Resources>
   <StackPanel Orientation="Horizontal" x:Name="sp1">
      <VisualStateManager.VisualStateGroups>
         <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal"/>
            <VisualState x:Name="MouseOver"/>
            <VisualState x:Name="Pressed">
               <Storyboard>
                 <ObjectAnimationUsingKeyFrames
                         Storyboard.TargetProperty="Background"
                         Storyboard.TargetName="sp1">
                    <DiscreteObjectKeyFrame KeyTime="0"
                            Value="{StaticResource PhoneBackgrounBrush}"/>
                 </ObjectAnimationUsingKeyFrames>
               </Storyboard>
            </VisualState>
         </VisualStateGroup>
       </VisualStateManager.VisualStateGroups>
       <Image Source="/Assets/Menu/wheretostay.png" Stretch="None"/>
       <TextBlock />
   </StackPanel>
 </ControlTemplate>

Problem

I have stack panel in hyperlink button on button click i have to change stack panel background ``` <HyperlinkButton Name="WhereToStayButton" Margin="0,0,0,0" Grid.Row="5" Click="WhereToStayButton_Click"> <HyperlinkButton.Template> <ControlTemplate TargetType="HyperlinkButton"> <StackPanel Orientation="Horizontal" Background="#EBEBEB" x:Name="sp1"> <Image Source="/Assets/Menu/wheretostay.png" Stretch="None"/> <TextBlock Text="{Binding Path=LocalizedResources.menu_where_stay, Source={StaticResource LocalizedStrings}}" VerticalAlignment="Center" Margin="5,10" FontSize="26" Foreground="Black" FontFamily="{StaticResource CustomLucidaGrandStyle}"/> </StackPanel> </ControlTemplate> </HyperlinkButton.Template> </HyperlinkButton> ```

Original source

Related problems