动态设置TargetName,在XAML中设置DoubleAnimationUsingKeyFrames

时间:2011-03-28 06:59:21

标签: wpf dynamic storyboard target

我想知道如何使用相同的动画动态更改目标名称

请找到以下是我的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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

H.B.说工作得很好。在XAML中放置故事板而没有像这样指定TargetName

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#我写了一个自定义函数来调用Animation。

RunStoryBoardFromName("OpacityUpAnim", "PopupGrid");

然后我在C#中称它为

<div class="myrow">
    <div class="logo">
      <img src="https://unsplash.it/150/150">
    </div>
    <div class="header-info">
      <div class="name">
        <p>
          Someperson
        </p>
      </div>
      <div class="description">
        <p>
          longer text goes heeeeeeeeeeeere.
        </p>
      </div>
    </div>
  </div>