.Net WPF – Create Animation Programmatically

5 Comments

This is a short code snippet on creating an animation in WPF through code.

In my Twitter app that I have been working on, I was recently doing some optimizations.  When I first did the app, I was new to WPF and knew nothing about animations.  After spending time in Silverlight, I used that knowledge to do some animations in WPF.

I wanted to do simple “fade-in” and “fade-out” animations when removing tweets from view and adding new tweets to the view.   And being the person that I am, I like doing this stuff in code.  While I like XAML a lot, I still like writing the code.  So here is how to do an animation that changes the Opacity property programmatically.

// because I am doing these as extension methods, they
//   will be available to all UIElement objects, which are
//   basically all controls that can be added to the GUI
public static class ControlAnimationExtensionMethods
{
    public static void FadeIn(this UIElement targetControl)
    {
        DoubleAnimation fadeInAnimation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1.5)));
        Storyboard.SetTarget(fadeInAnimation, targetControl);
        Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
        Storyboard sb = new Storyboard();
        sb.Children.Add(fadeInAnimation);
        sb.Begin();
    }

    public static void FadeOut(this UIElement targetControl)
    {
        DoubleAnimation fadeInAnimation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(1.5)));
        Storyboard.SetTarget(fadeInAnimation, targetControl);
        Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
        Storyboard sb = new Storyboard();
        sb.Children.Add(fadeInAnimation);
        sb.Begin();
    }
}

This code is useful when dynamically creating controls that you want to do animations on.

TextBlock tb = new TextBlock();
tb.Name = "textBlock1";
// set more property values

tb.FadeIn();

Tags: , , ,

5 Responses to “WPF – Create Animation Programmatically”

  1. Learn android phone programming Says:

    This makes sense

  2. Joleen Rutheford Says:

    Permit me to reveal exactly how I get my tweets page, facebook user profile or internet site trending on twitter 100 % free.

  3. Margrett Foard Says:

    Props for such a superb post, maintain up your fantastic work.

  4. Watch Live Football Streaming Says:

    I needed to create you the little note to be able to give thanks again over the lovely ideas you have shown here. This is certainly seriously open-handed with people like you to give publicly just what a number of people would have made available as an ebook to get some money on their own, mostly considering the fact that you could possibly have done it in case you wanted. The guidelines also served as the good way to understand that many people have the same fervor much like my very own to know the truth significantly more pertaining to this condition. I believe there are some more fun periods ahead for folks who read through your blog post.

  5. Billy Lianes Says:

    WE are praying everyday that we win aug.13th for the $100,000.00 a week for a entire year bless us please this would mean soooooooooooooo much to the Marsh Family!!!!! please dont let us all down!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Leave a Reply