black eyed pea
black eyed pea

Reputation: 427

How do I use a storyboard for more than 1 target object?

I have a c# class, let's call it TwoButtonsHolder. In this class, I have 2 UIElements I wanna animate using StoryBoards. Let's call them ButtonA and ButtonB. Because I wanna have the both of them animate at the same time, I decided to put all their animations in one Storyboard object (or is there a better way?). So right now I'm trying to create the storyboard object in code behind, but I am stuck at the part where I have to define the target property. Normally when I declare animations I use code like this to set the property

Storyboard.SetTargetProperty(myDoubleAnimation,
                             new PropertyPath(UIElement.OpacityProperty));

now I have the UIElement within the target Object of the type TwoButtonsHolder. How do I create a storyboard and set the animation target property to TwoButtonsHolder.ButtonA.UIElement.OpacityProperty? Is this bad design for using a single storyboard to animate multiple objects in order to have animation that runs at the same time? (Just FYI, I wanna try this in code behind, don't wanna go into XAML yet because I find it too complicated and hard to learn IMHO). Thank you

Edit: Just wanna add that the 2 buttons have different animations, the only reason I'm putting them in the same storyboard is for them to start animating at the same time

Upvotes: 3

Views: 4132

Answers (1)

Clemens
Clemens

Reputation: 128042

It is absolutely no bad design to have animations for multiple objects in one Storyboard.

You would write it somehow like this:

var animationA = new DoubleAnimation(...);
Storyboard.SetTarget(animationA, ButtonA);
Storyboard.SetTargetProperty(animationA, new PropertyPath(UIElement.OpacityProperty));

var animationB = new DoubleAnimation(...);
Storyboard.SetTarget(animationB, ButtonB);
Storyboard.SetTargetProperty(animationB, new PropertyPath(UIElement.OpacityProperty));

var storyboard = new Storyboard();
storyboard.Children.Add(animationA);
storyboard.Children.Add(animationB);
storyboard.Begin();

Upvotes: 9

Related Questions