ColorAnimation Class
Assembly: PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
An animation updates the value of a property over a period of time. An animation effect can be subtle, such as moving a Shape a few pixels left and right, or dramatic, such as enlarging an object to 200 times its original size while spinning it and changing its color. To create an animation in Windows Presentation Foundation (WPF), you associate an animation with an object's property value.
Target Values
The ColorAnimation class creates a transition between two target values. To set its target values, use its From, To, and By properties. The following table summarizes how the From, To, and By properties may be used together or separately to determine an animation's target values.
Properties specified | Resulting behavior |
|---|---|
The animation progresses from the value specified by the From property to the base value of the property being animated or to a previous animation's output value, depending on how the previous animation is configured. | |
The animation progresses from the value specified by the From property to the value specified by the To property. | |
The animation progresses from the value specified by the From property to the value specified by the sum of the From and By properties. | |
The animation progresses from the animated property's base value or a previous animation's output value to the value specified by the To property. | |
The animation progresses from the base value of the property being animated or a previous animation's output value to the sum of that value and the value specified by the By property. |
Note: |
|---|
If you set both the To and By properties, the To property takes precedence and the By property is ignored. |
To use other interpolation methods or animate between more than two target values, use a ColorAnimationUsingKeyFrames object.
For information about applying multiple animations to a single property, see Key-Frame Animations Overview.
Freezable Features
Because the ColorAnimation class inherits from Freezable, ColorAnimation objects gain several special features, which include the following: they can be declared as resources, shared among multiple objects, made read-only to improve performance, cloned, and made thread-safe. For more information about the different features provided by Freezable objects, see the Freezable Objects Overview.
This example shows how to animate the Color and Opacity of a SolidColorBrush.
The following example uses three animations to animate the Color and Opacity of a SolidColorBrush.
The first animation, a ColorAnimation, changes the brush's color to Gray when the mouse enters the rectangle.
The next animation, another ColorAnimation, changes the brush's color to Orange when the mouse leaves the rectangle.
The final animation, a DoubleAnimation, changes the brush's opacity to 0.0 when the left mouse button is pressed.
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Input; namespace Microsoft.Samples.Animation { /// <summary> /// This example shows how to animate the Opacity and Color /// properties of a SolidColorBrush. /// </summary> public class SolidColorBrushExample : Page { public SolidColorBrushExample() { Title = "SolidColorBrush Animation Example"; Background = Brushes.White; // Create a NameScope for the page so // that Storyboards can be used. NameScope.SetNameScope(this, new NameScope()); // Create a Rectangle. Rectangle aRectangle = new Rectangle(); aRectangle.Width = 100; aRectangle.Height = 100; // Create a SolidColorBrush to paint // the rectangle's fill. The Opacity // and Color properties of the brush // will be animated. SolidColorBrush myAnimatedBrush = new SolidColorBrush(); myAnimatedBrush.Color = Colors.Orange; aRectangle.Fill = myAnimatedBrush; // Register the brush's name with the page // so that it can be targeted by storyboards. this.RegisterName("MyAnimatedBrush", myAnimatedBrush); // // Animate the brush's color to gray when // the mouse enters the rectangle. // ColorAnimation mouseEnterColorAnimation = new ColorAnimation(); mouseEnterColorAnimation.To = Colors.Gray; mouseEnterColorAnimation.Duration = TimeSpan.FromSeconds(1); Storyboard.SetTargetName(mouseEnterColorAnimation, "MyAnimatedBrush"); Storyboard.SetTargetProperty( mouseEnterColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); Storyboard mouseEnterStoryboard = new Storyboard(); mouseEnterStoryboard.Children.Add(mouseEnterColorAnimation); aRectangle.MouseEnter += delegate(object sender, MouseEventArgs e) { mouseEnterStoryboard.Begin(this); }; // // Animate the brush's color to orange when // the mouse leaves the rectangle. // ColorAnimation mouseLeaveColorAnimation = new ColorAnimation(); mouseLeaveColorAnimation.To = Colors.Orange; mouseLeaveColorAnimation.Duration = TimeSpan.FromSeconds(1); Storyboard.SetTargetName(mouseLeaveColorAnimation, "MyAnimatedBrush"); Storyboard.SetTargetProperty( mouseLeaveColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); Storyboard mouseLeaveStoryboard = new Storyboard(); mouseLeaveStoryboard.Children.Add(mouseLeaveColorAnimation); aRectangle.MouseLeave += delegate(object sender, MouseEventArgs e) { mouseLeaveStoryboard.Begin(this); }; // // Animate the brush's opacity to 0 and back when // the left mouse button is pressed over the rectangle. // DoubleAnimation opacityAnimation = new DoubleAnimation(); opacityAnimation.To = 0.0; opacityAnimation.Duration = TimeSpan.FromSeconds(0.5); opacityAnimation.AutoReverse = true; Storyboard.SetTargetName(opacityAnimation, "MyAnimatedBrush"); Storyboard.SetTargetProperty( opacityAnimation, new PropertyPath(SolidColorBrush.OpacityProperty)); Storyboard mouseLeftButtonDownStoryboard = new Storyboard(); mouseLeftButtonDownStoryboard.Children.Add(opacityAnimation); aRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e) { mouseLeftButtonDownStoryboard.Begin(this); }; StackPanel mainPanel = new StackPanel(); mainPanel.Margin = new Thickness(20); mainPanel.Children.Add(aRectangle); Content = mainPanel; } } }
<!-- SolidColorBrushExample.xaml This example shows how to animate the Opacity and Color properties of a SolidColorBrush.--> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="SolidColorBrush Animation Example" Background="White"> <StackPanel Margin="20"> <!-- The Opacity and Color of the SolidColorBrush used to fill this rectangle is animated. --> <Rectangle Width="100" Height="100"> <Rectangle.Fill> <SolidColorBrush x:Name="MyAnimatedBrush" Color="Orange" /> </Rectangle.Fill> <Rectangle.Triggers> <!-- Animates the brush's color to gray When the mouse enters the rectangle. --> <EventTrigger RoutedEvent="Rectangle.MouseEnter"> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="MyAnimatedBrush" Storyboard.TargetProperty="Color" To="Gray" Duration="0:0:1" /> </Storyboard> </BeginStoryboard> </EventTrigger> <!-- Animates the brush's color to orange when the mouse leaves the rectangle. --> <EventTrigger RoutedEvent="Rectangle.MouseLeave"> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="MyAnimatedBrush" Storyboard.TargetProperty="Color" To="Orange" Duration="0:0:1" /> </Storyboard> </BeginStoryboard> </EventTrigger> <!-- Animates the brush's opacity when the left mouse button is pressed over the rectangle. --> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="MyAnimatedBrush" Storyboard.TargetProperty="Opacity" To="0.0" Duration="0:0:0.5" AutoReverse="True" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> </StackPanel> </Page>
For a more complete sample, which shows how to animate different types of brushes, see the Brushes Sample. For more information about animation, see the Animation Overview.
For consistency with other animation examples, the code versions of this example use a Storyboard object to apply their animations. However, when applying a single animation in code, it's simpler to use the BeginAnimation method instead of using a Storyboard. For an example, see How to: Animate a Property Without Using a Storyboard.
More Code
| How to: Animate a Property Without Using a Storyboard | This example shows one way to apply an animation to a property without using a Storyboard. |
| How to: Animate a Property by Using a Storyboard | This example shows how to use a Storyboard to animate properties. To animate a property by using a Storyboard, create an animation for each property that you want to animate and also create a Storyboard to contain the animations. |
| How to: Control an Animation using From, To, and By | A "From/To/By" or "basic animation" creates a transition between two target values (see Animation Overview for an introduction to different types of animations). To set the target values of a basic animation, use its From, To, and By properties. The following table summarizes how the From, To, and By properties may be used together or separately to determine an animation's target values. |
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Freezable
System.Windows.Media.Animation.Animatable
System.Windows.Media.Animation.Timeline
System.Windows.Media.Animation.AnimationTimeline
System.Windows.Media.Animation.ColorAnimationBase
System.Windows.Media.Animation.ColorAnimation
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: