2011-03-28 32 views
0

我想知道如何使用同樣的動畫dynamicaly更改目標名稱設置的TargetName動態和DoubleAnimationUsingKeyFrames在XAML

請看下面是我的XAML和C#代碼WPF的代碼

XAML代碼

<Storyboard x:Key="deepanshu"> 
    <DoubleAnimationUsingKeyFrames x:Name="gupta" 
            Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" 
            Storyboard.TargetName="image1"> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.641"/> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/> 
    </DoubleAnimationUsingKeyFrames> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" 
            Storyboard.TargetName="image1"> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.689"/> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/> 
    </DoubleAnimationUsingKeyFrames> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" 
            Storyboard.TargetName="image1"> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="-1"/> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/> 
    </DoubleAnimationUsingKeyFrames> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" 
            Storyboard.TargetName="image1"> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.5"/> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/> 
    </DoubleAnimationUsingKeyFrames> 
</Storyboard> 

C#

Storyboard sb = (Storyboard)FindResource("deepanshu"); 

現在如何將storyboaname從image1更改爲image2?

感謝 問候, Deepanshu

回答

1
Storyboard sb = (Storyboard)FindResource("deepanshu"); 
foreach (var animation in sb.Children) 
{ 
    Storyboard.SetTargetName(animation, "image2"); 
} 
0

什麼H.B.說工作得很好。在XAML把故事板沒有這樣

<Storyboard x:Key="OpacityUpAnim"> 
     <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:1.3"> 
      <DoubleAnimation.EasingFunction> 
       <CubicEase EasingMode="EaseOut" /> 
      </DoubleAnimation.EasingFunction> 
     </DoubleAnimation> 
</Storyboard> 

指定的TargetName爲C#我寫了一個自定義函數來調用動畫。

private void RunStoryBoardFromName(string animName, string targetName = null) 
    { 
     Storyboard storyBoard = (Storyboard)this.Resources[animName]; 
     if (targetName != null) 
     { 
      foreach (var anim in storyBoard.Children) 
      { 
       Storyboard.SetTargetName(anim, targetName); 
      } 
     } 
     storyBoard.Begin(); 
    } 

然後我把它叫做在C#一樣

RunStoryBoardFromName("OpacityUpAnim", "PopupGrid");