Click here to Skip to main content
15,887,821 members
Articles / Desktop Programming / WPF

WPF: Beware of Zombie Animations Freezing Your Properties

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
10 Sep 2012Apache 8.5K   3   1
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...

C#
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:

C#
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.

C#
// 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();

This article was originally posted at http://www.ikriv.com/blog?p=1077

License

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


Written By
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

 
GeneralMy vote of 5 Pin
Pratik Bhuva19-Nov-19 20:51
professionalPratik Bhuva19-Nov-19 20:51 
I can't thank you enough.
I am converting an application from silverlight to WPF and I spent almost a day to figure out an issue and then your article came to rescue.
Thank you so much.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.