How do I use a storyboard for more than one target?

I have a C # class, call it TwoButtonsHolder

. In this class, I have 2 UIElements

I want to animate with StoryBoards. Name them ButtonA

and ButtonB

. Since I want both of them to 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 a storyboard object in the code behind, but I'm stuck at the part where I need to define the target property. Usually, when I declare an animation, I use code like this to set the property

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

      

I now have a UIElement in a target of type TwoButtonsHolder. How do I create a storyboard and set the target animation property to TwoButtonsHolder.ButtonA.UIElement.OpacityProperty

? Is it bad design to use one storyboard to animate multiple objects so that the animation works at the same time? (Just FYI, I want to try this in the code behind, don't want to go into XAML yet because I find it too complicated and difficult to learn IMHO). Thanks you

Edit: just want to add that the two buttons have different animations, the only reason I put them on the same storyboard is to start animating at the same time

+3


source to share


1 answer


It's not a bad design to have animations for multiple objects in one storyboard.

You could write it 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();

      

+9


source







All Articles