2014-02-25 255 views
1

我在超链接按钮堆栈面板上的按钮,点击我要改变堆栈面板背景背景颜色变化

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

回答

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

这是Windows Phone 8的错误是:在“可附加属性触发器不识别zed或不可访问“plz帮助我 –

+0

您可以在Windows Phone 7中使用'VisualStates',如[这里](http://stackoverflow.com/a/8117589/632337)所述。创建按下的视觉状态。 –

+0

我已经更新了Windows Phone 7的答案。请看看它。 –

6

试试这个

使用这个名称空间using System.Windows.Media;和按钮单击事件写这

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

无法在后台获取堆栈面板名称,因为它存在于模板中 –

+0

检查此答案如何在模板内访问控制http://stackoverflow.com/questions/19379946/how-to-access-a-control-within-data -Template-从代码隐藏 –

0

随着罗希特说,运用可视状态才达到你的要求..,

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