Create a Storyboard Programatically

I’ve just created my first Storyboard outside of Blend so I thought I break down the code and save it for reference

            // Create a duration of .5 seconds.
            Duration duration = new Duration(TimeSpan.FromMilliseconds(500));

            //Create two DoubleAnimationsUsingKeyFrame and set their properties.
            DoubleAnimationUsingKeyFrames myDoubleAnimation1 = new DoubleAnimationUsingKeyFrames();
            myDoubleAnimation1.Duration = duration;

            //Create an EasingDoubleKeyFrame
            EasingDoubleKeyFrame WidthChange = new EasingDoubleKeyFrame();
            WidthChange.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
            WidthChange.Value = DblHeight;
            CircleEase EsEase = new CircleEase();
            EsEase.EasingMode = EasingMode.EaseInOut;
            WidthChange.EasingFunction = EsEase;
            myDoubleAnimation1.KeyFrames.Add(WidthChange);

            Storyboard sb = new Storyboard();
            sb.Duration = duration;

            sb.Children.Add(myDoubleAnimation1);

            //I don't know why but Targets are set with a static method in the Storyboard class
            Storyboard.SetTarget(myDoubleAnimation1, AnimationTarget);
            Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(FrameworkElement.Width)"));

            return sb;
Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: