开发者

Set TargetName dynamically and DoubleAnimationUsingKeyFrames in XAML

i want to know how to change target name dynamicaly with same animation

Please find Below is my code of WPF for xaml and c# code

XAML code

<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");

Now how to change storyboaname from image1 to image2?

Thanks Regards, Deepanshu


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


What H.B. said is working perfectly fine. In XAML put storyboard without specifying TargetName like this

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

for C# I wrote a custom function to call Animation.

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();
    }

then I called it in C# like

RunStoryBoardFromName("OpacityUpAnim", "PopupGrid");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜