2014-02-28 31 views
1

我想使用故事板來更改我正在使用的按鈕的邊界顏色。問題是我不知道如何開始我的故事板。當我點擊元素時,我希望它改變顏色。我目前得到的代碼是:激活故事板WP8

<Button BorderBrush="Transparent" BorderThickness="0"> 
     <Button.Template> 
      <ControlTemplate> 
       <Path x:Name="CountryUser" Stretch="Fill" StrokeThickness="{StaticResource StrokeUserControl}" StrokeLineJoin="Round" Stroke="Black" Data="{Binding CountryView.MapData}" Fill="{StaticResource CountryBackground}"/> 
      </ControlTemplate> 
     </Button.Template> 

     <Button.Triggers> 
      <EventTrigger RoutedEvent="Click"> 
       <BeginStoryboard> 
        <Storyboard x:Name="StoryBoard1"> 
         <ColorAnimation Storyboard.TargetName="CountryUser" Storyboard.TargetProperty="Stroke" From="Black" To="Blue"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Button.Triggers> 

    </Button> 

單擊我使用eas event.trigger不起作用。

+0

,請檢查我的更新回答。謝謝。 – asitis

回答

2

如果你想在故事板,以改變你塑造你的按鈕路徑的顏色,你必須將故事板移動到路徑資源:

<Button x:Name="Button1" BorderThickness="0" BorderBrush="Transparent"> 
     <Button.Template> 
      <ControlTemplate x:Name="Control"> 
       <Path x:Name="CountryUser" Style="{StaticResource style_ColorButton}" Data="{Binding CountryView.MapData}" Fill="{StaticResource CountryBackground}"> 
        <Path.Resources> 
         <Storyboard x:Name="StoryBoard1"> 
          <ColorAnimation Storyboard.TargetName="CountryUser" Storyboard.TargetProperty="(Stroke).(SolidColorBrush.Color)" To="Blue" Duration="0"/> 
         </Storyboard> 
        </Path.Resources> 
       </Path> 
      </ControlTemplate> 
     </Button.Template> 

從後面的代碼,你應該找到按鈕,一旦找到該按鈕,使用VisualTreeHelper在按鈕中查找路徑,然後從後面的代碼開始創建故事板。由於故事板設置的資源,你應該做到以下幾點一旦你找到的路徑:

Storyboard sb = h.Resources["StoryBoard1"] as Storyboard; 

這裏如果你想要去與你的當前實現h是路徑

+0

謝謝,它的工作 – JonasN89

1

Silverlight僅支持Loaded作爲RoutedEvent的值。 的選項有:

  • 定義你的故事板作爲一種資源,增加在Button.Click一個處理程序,並開始落後
  • 代碼中的動畫使用VisualStateManagerPressed狀態Button(內Template

Source (see Remarks section)

+2

我建議改變視覺狀態,而不是添加故事板。 –

+0

@lisp我跟着你在最後添加的源代碼,在這裏它說我應該從資源字典中檢索我的故事板,我不知道如何做到這一點,有什麼幫助? – JonasN89

+0

@ JonasN89對''Page'ResourceDictionary'中定義的'Storyboard'使用'x:Name =「ButtonTapStoryboard」': 'var storyboard =(Storyboard)this.Resources [「ButtonTapStoryboard」]; storyboard.Begin();' – lisp

0

試試看看這個代碼。

添加這些命名空間

的xmlns:交互= 「使用:Microsoft.Xaml.Interactivity」 的xmlns:核心= 「使用:Microsoft.Xaml.Interactions.Core」 的xmlns:媒體=「使用: Microsoft.Xaml.Interactions.Media」

編輯

使用TemplateBinding上點擊更改路徑的顏色。

<Page.Resources> 
    <Storyboard x:Name="ButtonStoryboard"> 
     <ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(Control.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="button" d:IsOptimized="True"/> 
    </Storyboard> 
</Page.Resources> 

    <Button x:Name="button" Background="Blue" BorderBrush="Black" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="864,248,0,0" Height="90" Width="214"> 
     <Button.Template> 
      <ControlTemplate> 
       <Grid Background="White"> 
        <Path x:Name="CountryUser" Stretch="Fill" Height="50" Width="50" StrokeThickness="15" StrokeLineJoin="Round" Stroke="{TemplateBinding BorderBrush}" Data="M13.5,14.25v-5h13v5h-2v-3h-9v3H13.5z M9,14.75h22v11.5h-4.5v4.5h-13v-4.5H9V14.75z M28.5,23.75v-6.5h-17 
    v6.5H28.5z M13.5,22.75v-2.5h13v2.5H13.5z" Fill="{TemplateBinding BorderBrush}"/> 
       </Grid> 
      </ControlTemplate> 
     </Button.Template> 
     <Interactivity:Interaction.Behaviors> 
      <Core:EventTriggerBehavior EventName="Tapped"> 
       <Media:ControlStoryboardAction Storyboard="{StaticResource ButtonStoryboard}"/> 
      </Core:EventTriggerBehavior> 
     </Interactivity:Interaction.Behaviors> 
    </Button> 
+0

我嘗試添加命名空間,但是當我添加代碼時,它無法找到可附加屬性,任何想法爲什麼? – JonasN89

+0

您是否添加了2個Microsoft.xaml.Interations&。 Microsoft.xaml.Interactivity? – asitis

+0

@ JonasN89您應該右鍵單擊項目中的視圖和視圖模型。按添加參考。在新窗口中按下框架,選擇擴展。你會發現System.Windows.Interactivity和System.Windows.Interactions。隨着這些添加,你應該能夠跟隨asitis的例子 – JTIM

1

沒有故事板需要

您需要一個帶按鈕的可視狀態發揮,你可以使用樣式的幫助

只要看看下面的樣式

<Style x:Key="style_ColorButton" TargetType="Button"> 
     <Setter Property="Background" Value="Black"/> 
     <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
     <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/> 
     <Setter Property="Padding" Value="10,3,10,5"/> 
     <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="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Green"/>        
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </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> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0"> 
          <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

以上VisualState風格「按下」處理您的要求,我已經在那裏給了一個圖像源。

使用此樣式的按鈕。

<Button Height="40" Width="40" BorderThickness="0" Name="btnAcceptCrop" Click="btnAcceptCrop_Click" Style="{StaticResource style_ColorButton}" Background="Black" ForeGround="White"/> 

設置樣式代碼

btnAcceptCrop.Style = (Style)App.Current.Resources["style_ColorButton"]; 

在你的頁面在頁面

聲明一個風格下的所有空間聲明

只是做一個標記

<phone:PhoneApplicationPage.Resources> 
</phone:PhoneApplicationPage.Resources> 

並在裏面聲明你的風格。

希望這會有所幫助。