---
title: "Animation Tips and Tricks"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "troubleshooting [WPF], animation"
- "animations [WPF], FillBehavior property"
- "troubleshooting animation [WPF]"
- "animating objects [WPF], troubleshooting"
- "animation tips and tricks [WPF]"
- "tips and tricks [WPF], animation"
- "performance troubleshooting [WPF], animation"
- "animations [WPF], use of system resources"
ms.assetid: e467796b-d5d4-45a6-a108-8c5d7ff69a0f
---
# Animation Tips and Tricks
When working with animations in WPF, there are a number of tips and tricks that can make your animations perform better and save you frustration.
## General Issues
### Animating the Position of a Scroll Bar or Slider Freezes It
If you animate the position of a scroll bar or slider using an animation that has a of (the default value), the user will no longer be able to move the scroll bar or slider. That's because, even though the animation ended, it's still overriding the target property's base value. To stop the animation from overriding the property's current value, remove it, or give it a of . For more information and an example, see [Set a Property After Animating It with a Storyboard](how-to-set-a-property-after-animating-it-with-a-storyboard.md).
### Animating the Output of an Animation Has No Effect
You can't animate an object that is the output of another animation. For example, if you use an to animate the of a from a to a , you can't animate any properties of the or .
### Can't Change the Value of a Property after Animating it
In some cases, it might appear that you can't change the value of a property after it's been animated, even after the animation has ended. That's because, even though the animation ended, it's still overriding the property's base value. To stop the animation from overriding the property's current value, remove it, or give it a of . For more information and an example, see [Set a Property After Animating It with a Storyboard](how-to-set-a-property-after-animating-it-with-a-storyboard.md).
### Changing a Timeline Has No Effect
Although most properties are animatable and can be data bound, changing the property values of an active seems to have no effect. That's because, when a is begun, the timing system makes a copy of the and uses it to create a object. Modifying the original has no effect on the system's copy.
For a to reflect changes, its clock must be regenerated and used to replace the previously created clock. Clocks are not regenerated for you automatically. The following are several ways to apply timeline changes:
- If the timeline is or belongs to a , you can make it reflect changes by reapplying its storyboard using a or the method. This has the side effect of also restarting the animation. In code, you can use the method to advance the storyboard back to its previous position.
- If you applied an animation directly to a property using the method, call the method again and pass it the animation that has been modified.
- If you are working directly at the clock level, create and apply a new set of clocks and use them to replace the previous set of generated clocks.
For more information about timelines and clocks, see [Animation and Timing System Overview](animation-and-timing-system-overview.md).
### FillBehavior.Stop Doesn't Work as Expected
There are times when setting the property to seems to have no effect, such as when one animation "hands off" to another because it has a setting of .
The following example creates a , a and a . The will be animated to move the around the .
[!code-xaml[AnimationTipsAndTricksSample_snip#FillBehaviorTipAnimatedObject](~/samples/snippets/csharp/VS_Snippets_Wpf/AnimationTipsAndTricksSample_snip/CSharp/FillBehaviorTip.xaml#fillbehaviortipanimatedobject)]
The examples in this section use the preceding objects to demonstrate several cases where the property doesn't behave as you might expect it to.
#### FillBehavior="Stop" and HandoffBehavior with Multiple Animations
Sometimes it seems as though an animation ignores its property when it is replaced by a second animation. Take the following example, which creates two objects and uses them to animate the same shown in the preceding example.
The first , `B1`, animates the property of the from 0 to 350, which moves the rectangle 350 pixels to the right. When the animation reaches the end of its duration and stops playing, the property reverts to its original value, 0. As a result, the rectangle moves to the right 350 pixels and then jumps back to its original position.
[!code-xaml[AnimationTipsAndTricksSample_snip#FillBehaviorTipStoryboardB1Button](~/samples/snippets/csharp/VS_Snippets_Wpf/AnimationTipsAndTricksSample_snip/CSharp/FillBehaviorTip.xaml#fillbehaviortipstoryboardb1button)]
The second , `B2`, also animates the property of the same . Because only the property of the animation in this is set, the animation uses the current value of the property it animates as its starting value.
[!code-xaml[AnimationTipsAndTricksSample_snip#FillBehaviorTipStoryboardB2Button](~/samples/snippets/csharp/VS_Snippets_Wpf/AnimationTipsAndTricksSample_snip/CSharp/FillBehaviorTip.xaml#fillbehaviortipstoryboardb2button)]
If you click the second button while the first is playing, you might expect the following behavior:
1. The first storyboard ends and sends the rectangle back to its original position, because the animation has a of .
2. The second storyboard takes effect and animates from the current position, which is now 0, to 500.
**But that's not what happens.** Instead, the rectangle does not jump back; it continues moving to the right. That's because the second animation uses the current value of the first animation as its starting value and animates from that value to 500. When the second animation replaces the first because the is used, the of the first animation does not matter.
#### FillBehavior and the Completed Event
The next examples demonstrate another scenario in which the seems to have no effect. Again, the example uses a Storyboard to animate the property of the from 0 to 350. However, this time the example registers for the event.
[!code-xaml[AnimationTipsAndTricksSample_snip#FillBehaviorTipStoryboardCButton](~/samples/snippets/csharp/VS_Snippets_Wpf/AnimationTipsAndTricksSample_snip/CSharp/FillBehaviorTip.xaml#fillbehaviortipstoryboardcbutton)]
The event handler starts another that animates the same property from its current value to 500.
[!code-csharp[AnimationTipsAndTricksSample_snip#FillBehaviorTipStoryboardC1CompletedHandler](~/samples/snippets/csharp/VS_Snippets_Wpf/AnimationTipsAndTricksSample_snip/CSharp/FillBehaviorTip.xaml.cs#fillbehaviortipstoryboardc1completedhandler)]
[!code-vb[AnimationTipsAndTricksSample_snip#FillBehaviorTipStoryboardC1CompletedHandler](~/samples/snippets/visualbasic/VS_Snippets_Wpf/AnimationTipsAndTricksSample_snip/VisualBasic/FillBehaviorTip.xaml.vb#fillbehaviortipstoryboardc1completedhandler)]
The following is the markup that defines the second as a resource.
[!code-xaml[AnimationTipsAndTricksSample_snip#FillBehaviorTipResources](~/samples/snippets/csharp/VS_Snippets_Wpf/AnimationTipsAndTricksSample_snip/CSharp/FillBehaviorTip.xaml#fillbehaviortipresources)]
When you run the , you might expect the property of the to animate from 0 to 350, then revert to 0 after it completes (because it has a setting of ), and then animate from 0 to 500. Instead, the animates from 0 to 350 and then to 500.
That's because of the order in which WPF raises events and because property values are cached and are not recalculated unless the property is invalidated. The event is processed first because it was triggered by the root timeline (the first ). At this time, the property still returns its animated value because it hasn't been invalidated yet. The second uses the cached value as its starting value and begins animating.
## Performance
### Animations Continue to Run After Navigating Away from a Page
When you navigate away from a that contains running animations, those animations will continue to play until the is garbage collected. Depending on the navigation system you're using, a page that you navigate away from might stay in memory for an indefinite amount of time, all the while consuming resources with its animations. This is most noticeable when a page contains constantly running ("ambient") animations.
For this reason, it's a good idea to use the event to remove animations when you navigate away from a page.
There are different ways to remove an animation. The following techniques can be used to remove animations that belong to a .
- To remove a you started with an event trigger, see [How to: Remove a Storyboard](/previous-versions/dotnet/netframework-3.5/ms749412(v=vs.90)).
- To use code to remove a , see the method.
The next technique may be used regardless of how the animation was started.
- To remove animations from a specific property, use the method. Specify the property being animated as the first parameter, and `null` as the second. This will remove all animation clocks from the property.
For more information about the different ways to animate properties, see [Property Animation Techniques Overview](property-animation-techniques-overview.md).
### Using the Compose HandoffBehavior Consumes System Resources
When you apply a , , or to a property using the , any objects previously associated with that property continue to consume system resources; the timing system will not remove these clocks automatically.
To avoid performance issues when you apply a large number of clocks using , you should remove composing clocks from the animated property after they complete. There are several ways to remove a clock.
- To remove all clocks from a property, use the or method of the animated object. Specify the property being animated as the first parameter, and `null` as the second. This will remove all animation clocks from the property.
- To remove a specific from a list of clocks, use the property of the to retrieve a , then call the method of the . This is typically done in the event handler for a clock. Note that only root clocks can be controlled by a ; the property of a child clock will return `null`. Note also that the event will not be called if the effective duration of the clock is forever. In that case, the user will need to determine when to call .
This is primarily an issue for animations on objects that have a long lifetime. When an object is garbage collected, its clocks will also be disconnected and garbage collected.
For more information about clock objects, see [Animation and Timing System Overview](animation-and-timing-system-overview.md).
## See also
- [Animation Overview](animation-overview.md)