0

I have multiple (dynamically created) controls in a grid (labels, buttons), and one storyboard! I can assign a storyboard to one control, but I want to apply the storyboard's animation in each controls...how can I do this?

My code:

Storyboard myStoryboard1 = new Storyboard();
Label label1 = new Label();
Label label2 = new Label();
Label label3 = new Label();
...
Storyboard.SetTarget(myAnimation2, Label1);
Storyboard.SetTarget(myAnimation2, Label2);
Storyboard.SetTarget(myAnimation2, Label3);

This code is set the animation only to the Label3...

Thanks!

KHFC
  • 3
  • 1
  • Sorry, I missed it... myAnimation2 is a DoubleAnimationUsingKeyFrames, and I add the animation to the storyboard... – KHFC Jun 02 '12 at 21:04

1 Answers1

0

The best way to do this is by making one or multiple styles for all the control types that you want to animate and base them all on a single style that contains the storyboard/animation.

Then, after you create/add a control just set its style (or make the styles the default styles)

var newLabel = new Label();
newLabel.Style = this.TryFindResource("controlStyle") as Style;
Emond
  • 50,210
  • 11
  • 84
  • 115
  • Thanks for your reply! Ok I understand, I need a style... but how can I create a style from code(not XAML) which is contain my animation? I can't found any example for this... – KHFC Jun 03 '12 at 10:53
  • So, I have the storyboard and a style. I add the style to the controls. But how can I link the storyboard to the style? – KHFC Jun 03 '12 at 11:01
  • When do you want the storyboard to be played? – Emond Jun 03 '12 at 11:55
  • Ah, sorry in that case the style won't help. You'll need to keep the storyboard separate (you could put it in the window or app resources). See this question http://stackoverflow.com/questions/1654355/define-animations-and-triggers-as-reusable-resource to see how to reuse an animation/storyboard – Emond Jun 03 '12 at 17:30
  • Thx, I saw the linked topic and try to apply it... I didn't think that link an animation to the controls is so complicated... – KHFC Jun 04 '12 at 16:39