Dynamically Changing TargetName DoubleAnimation c# xaml

66 Views Asked by At

im searching for a solution to change the TargetNameproperty in my code behind. in total i need 9 different images / animations to happen. in the example below there are just 2.

here is the XAML

<Storyboard x:Name="MyAnimationBoard1" x:Key="MyAnimationBoard">
                    <DoubleAnimationUsingKeyFrames
                        Storyboard.TargetName="Anim1"
                        Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
                        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.5"/>
                        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:0.10"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.20"/>
                        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:0.30"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.40"/>
                    </DoubleAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames
                        Storyboard.TargetName="Anim2"
                        Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
                        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:4"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.5"/>
                        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:4.10"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.20"/>
                        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:4.30"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.40"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>

Here you find the way i call the animations to begin in code behind

if (timesTicked == 59)
            {
                string voedsel = "Food1";
                Animate.AnimateFood(voedsel);
                //Would be nice to call the correct tagretname first.
                MyAnimationBoard1.Begin();
            }
            else if (timesTicked == 52)
            {
                string voedsel = "Food3";
                Animate.AnimateFood(voedsel);
                Debug.WriteLine("Tweede animatie");
                
            }
            else if (timesTicked == 46)
            {
                string voedsel = "Food8";
                Animate.AnimateFood(voedsel);
                Debug.WriteLine("Derde animatie");
            }
1

There are 1 best solutions below

0
On BEST ANSWER

You could use Storyboard.SetTargetName(Timeline, String) Method to change the value of TargetName property of a DoubleAnimation dynamically. You need to add a Name for every DoubleAnimationUsingKeyFrames.

For example:

<Storyboard x:Name="MyAnimationBoard1" x:Key="MyAnimationBoard">
    <DoubleAnimationUsingKeyFrames x:Name="DoubleAnimation1"
                Storyboard.TargetName="Anim1"
                Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.5"/>
        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:0.10"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.20"/>
        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:0.30"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.40"/>
    </DoubleAnimationUsingKeyFrames>

    <DoubleAnimationUsingKeyFrames x:Name="DoubleAnimation2"
                Storyboard.TargetName="Anim2"
                Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:4"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.5"/>
        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:4.10"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.20"/>
        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:4.30"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.40"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>
//
private void Button_Click(object sender, RoutedEventArgs e)
{
    MyAnimationBoard1.Stop();
    Storyboard.SetTargetName(DoubleAnimation1, "Anim2");
    Storyboard.SetTargetName(DoubleAnimation2, "Anim3");
    MyAnimationBoard1.Begin();
}