stackpanel中的背景颜色变化

时间:2014-02-25 04:52:29

标签: wpf windows-phone-7

我在按钮上单击超链接按钮时的堆栈面板我必须更改堆栈面板背景

              <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>

3 个答案:

答案 0 :(得分:7)

试试这个

使用此名称空间using System.Windows.Media;并在按钮点击事件中写下此

private void WhereToStayButton_Click(object sender, RoutedEventArgs e)
{
   stackpanelname.Background = new SolidColorBrush(Colors.Red);
}

答案 1 :(得分:2)

您可以通过在Click事件触发器上应用Storyboard来实现这一点

<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>

对于Windows Phone 7,请使用 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>

答案 2 :(得分:0)

正如Rohit所说,利用视觉状态来达到你的要求..,

<ControlTemplate TargetType="HyperlinkButton">
    <StackPanel Orientation="Horizontal"  Background="#EBEBEB" x:Name="sp1">
          <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal"/>
                <VisualState x:Name="MouseOver"/>
                <VisualState x:Name="Pressed">
                     <Storyboard>
                              <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="sp1">
                        <EasingColorKeyFrame KeyTime="0" Value="#FFE91818"/>
                                    </ColorAnimationUsingKeyFrames>
                     </Storyboard>
                </VisualState>
                <VisualState x:Name="Disabled"/>
            </VisualStateGroup>
         </VisualStateManager.VisualStateGroups>
       <Image Source="/Assets/Menu/wheretostay.png"  Stretch="None"/>
       <TextBlock />
   </StackPanel>
</ControlTemplate>