Click here to Skip to main content
Click here to Skip to main content
Technical Blog

WPF: Beware of zombie animations freezing your properties

, 10 Sep 2012 Apache
Rate this:
Please Sign up or sign in to vote.
Preventing zombie animations from freezing your properties.

I spent some time today trying to understand why my control’s size and/or position would refuse to change. A statement like

control.Width = 42;

would have absolutely no effect: the Width retained whatever value it had before, not 42. Worse yet, this was happening somewhat randomly.

Long story short, the problem was animations. By default the animation (StoryBoard) will hold animated property at the final value, even long after it is finished. You can assign other values all you want, that will be ignored. I kinda remembered that in the back of my head, but it took me some time until it clicked.

If the property was ever animated, assigning to it would have no effect. If, however, the property was not animated, assignment will work just fine. Hence the “randomness”.

To prevent evil zombie animations from holding on to your property, all you need to do is to set

storyBoard.FillBehavior = FillBehavior.Stop;

This applies equally to WPF and Silverlight. For that, however, you might be punished with flicker, as the property’s value will suddenly jump in the end of the animation. To avoid the flicker you may pre-assign the final value just before you start the animation.

// pseudo-code, not fully tested!

var sb = new Storyboard() { 
              Duration = TimeSpan.FromSeconds(1.0), 
              FillBehavior = FillBehavior.Stop };

var animation = new DoubleAnimation { 
              From = intialWidth, To = finalWidth, 
              Duration = sb.Duration };

sb.SetTarget(animation, control);
sb.SetTargetProperty(animation, new PropertyPath("Width"));

control.Width = finalWidth; // avoid flicker in the end of the animation
sb.Begin();

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

Ivan Krivyakov
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.
 
Please visit my web site: www.ikriv.com.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 10 Sep 2012
Article Copyright 2012 by Ivan Krivyakov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid