PETZOLD BOOK BLOG

Charles Petzold on writing books, reading books, and exercising the internal UTM


Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

WPF Animations in Code without Storyboards

Septermber 26, 2007
New York, N.Y.

In XAML, you define animations in storyboards. You can also use storyboards in code, but they're rather messy and in most cases completely unnecessary.

I mention this because many questioners (and even some answerers) in the MSDN WPF Forum seem to be unaware of the very powerful and very easy BeginAnimation method. You can't use BeginAnimation in XAML, but it's definitely the way to go when you're defining and triggering animations in code.

The following WPF classes define a BeginAnimation method:

These base classes account for quite a large chunk of WPF!

You call BeginAnimation on the object that has the property you want to animate. The first argument to BeginAnimation is the fully-qualified named of the dependency property to animate. For example, suppose you have a Button named btn and you want to animate the FontSize property. You call:

Of course, the Button class inherits the FontSize property and FontSizeProperty dependency property from Control.

The only other required argument to BeginAnimation is an object of type AnimationTimeline, which is a parent class to DoubleAnimation (which you'd probably use in this example) and all the other animation classes. These classes also have a Completed event if you want to be notified when the animation has completed. (This seems to be something else that questioners in the WPF Forum seem to need.)

Here's a complete program that animates the background color of a window:

Of course, you can also use BeginAnimation in any class you write that derives from one of the classes I've listed above. You really just need to back the property with a dependency property. This is why you should be very skeptical about anyone who indicates that dependency properties aren't a big deal in WPF programming.


Comments:

Charles,

Nice to see that you now accept comments on your blog! I remember you got a lot of shtick about this during the Applications = Code + Markup vs. other books saga...

If you are still accepting ideas for future blog entries, then maybe you would consider writing a blog entry on how to use C# to do animation from within Silverlight for those of us that prefer to write code using simple text editor.

Best Regards,

Anthony

— Anthony Tarlano, Fri, 28 Sep 2007 14:54:05 -0400 (EDT)

Charles,

FYI- Here http://tinyurl.com/3bcc9z is a blog entry I just found for using silverlight without VS.

Best Regards,

Anthony

— Anthony Tarlano, Sat, 29 Sep 2007 17:13:41 -0400 (EDT)

Charles,

Incase you haven't seen it, you may also want to check out this .NET Reflector Silverlight browswer plugin:

Best Regards,

Anthony

— Anthony, Fri, 12 Oct 2007 05:59:55 -0400 (EDT)


Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

(c) Copyright Charles Petzold
www.charlespetzold.com