2014-02-25 312 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>