Is it possible to start same animation for differe

2019-08-08 17:42发布

问题:

I have a story board with double animation,

<Storyboard x:Name="Storyboard1">
        <DoubleAnimation From="0" To="200" Duration="0:0:2" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)">

        </DoubleAnimation>
    </Storyboard>

and I just adding TargetNameProperty dynamically,

 private void set_animation_for_images(string target_name)
    {
        Storyboard1.Stop();

        Storyboard1.SetValue(Storyboard.TargetNameProperty, target_name);

        Storyboard1.Begin();
    }

Here is it possible to call this set_animation_for_images() method continuously for different image at same starting time?

回答1:

Because you only have the one storyboard, you can only have it targeting one element at a time. To target multiple images at a time, you may need to generate the whole thing in code:

private void animateImage(string target)
{
     Storyboard testStoryboard = new Storyboard();
     ...

     testStoryboard.SetValue(Storyboard.TargetNameProperty, target_name);
     testStoryboard.Begin();
}

If all the images are in a container, you could always target the container itself, but that might not get the effect you want.