
{"id":1077,"date":"2012-09-06T17:02:53","date_gmt":"2012-09-06T21:02:53","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1077"},"modified":"2012-09-06T17:02:53","modified_gmt":"2012-09-06T21:02:53","slug":"wpf-beware-of-zombie-animations-freezing-your-properties","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1077","title":{"rendered":"WPF: Beware of zombie animations freezing your properties"},"content":{"rendered":"<p>I spent some time today trying to understand why my control&#8217;s size and\/or position would refuse to change. A statement like<\/p>\n<pre><code>control.Width = 42;<\/code><\/pre>\n<p>would have absolutely no effect: the <code>Width<\/code> retained whatever value it had before, not 42. Worse yet, this was happening somewhat randomly.<\/p>\n<p>Long story short, the problem was animations. By default the animation (<code>StoryBoard<\/code>) 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.<\/p>\n<p>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 &#8220;randomness&#8221;.<\/p>\n<p>To prevent evil zombie animations from holding on to your property, all you need to do is to set<\/p>\n<p><code>storyBoard.FillBehavior = FillBehavior.Stop;<\/code><\/p>\n<p>This applies equally to WPF and Silverlight. For that, however, you might be punished with flicker, as the property&#8217;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.<\/p>\n<pre><code>\/\/ pseudo-code, not fully tested!\n\nvar sb = new Storyboard() {\n              Duration = TimeSpan.FromSeconds(1.0),\n              FillBehavior = FillBehavior.Stop };\n\nvar animation = new DoubleAnimation {\n              From = intialWidth, To = finalWidth,\n              Duration = sb.Duration };\n\nsb.SetTarget(animation, control);\nsb.SetTargetProperty(animation, new PropertyPath(\"Width\"));\n\ncontrol.Width = finalWidth; \/\/ avoid flicker in the end of the animation\nsb.Begin();<\/code><\/pre>\n<p><a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I spent some time today trying to understand why my control&#8217;s size and\/or position would refuse to change. A statement like control.Width = 42; would have absolutely no effect: the <a href=\"https:\/\/ikriv.com\/blog\/?p=1077\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4,12],"tags":[],"class_list":["entry","author-ikriv","post-1077","post","type-post","status-publish","format-standard","category-hack","category-wpf"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1077"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1077\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}