Posts Tagged ‘ Storyboard ’

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;