ColorAnimation Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Animates the value of a Color property between two target values using linear interpolation over a specified Duration.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.Media.Animation.Timeline
      System.Windows.Media.Animation.ColorAnimation

Namespace:  System.Windows.Media.Animation
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public NotInheritable Class ColorAnimation _
    Inherits Timeline
public sealed class ColorAnimation : Timeline
<ColorAnimation .../>

The ColorAnimation type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone ColorAnimation Initializes a new instance of the ColorAnimation class.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone AutoReverse Gets or sets a value that indicates whether the timeline plays in reverse after it completes a forward iteration. (Inherited from Timeline.)
Public propertySupported by Silverlight for Windows Phone BeginTime Gets or sets the time at which this Timeline should begin. (Inherited from Timeline.)
Public propertySupported by Silverlight for Windows Phone By Gets or sets the total amount by which the animation changes its starting value.
Public propertySupported by Silverlight for Windows Phone Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertySupported by Silverlight for Windows Phone Duration Gets or sets the length of time for which this timeline plays, not counting repetitions. (Inherited from Timeline.)
Public propertySupported by Silverlight for Windows Phone EasingFunction Gets or sets the easing function applied to this animation.
Public propertySupported by Silverlight for Windows Phone FillBehavior Gets or sets a value that specifies how the animation behaves after it reaches the end of its active period. (Inherited from Timeline.)
Public propertySupported by Silverlight for Windows Phone From Gets or sets the animation's starting value.
Public propertySupported by Silverlight for Windows Phone RepeatBehavior Gets or sets the repeating behavior of this timeline. (Inherited from Timeline.)
Public propertySupported by Silverlight for Windows Phone SpeedRatio Gets or sets the rate, relative to its parent, at which time progresses for this Timeline. (Inherited from Timeline.)
Public propertySupported by Silverlight for Windows Phone To Gets or sets the animation's ending value.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetAnimationBaseValue Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Events

  Name Description
Public eventSupported by Silverlight for Windows Phone Completed Occurs when the Storyboard object has completed playing. (Inherited from Timeline.)

Top

Fields

  Name Description
Public fieldStatic memberSupported by Silverlight for Windows Phone ByProperty Identifies the By dependency property.
Public fieldStatic memberSupported by Silverlight for Windows Phone EasingFunctionProperty Identifies the EasingFunction dependency property.
Public fieldStatic memberSupported by Silverlight for Windows Phone FromProperty Identifies the From dependency property.
Public fieldStatic memberSupported by Silverlight for Windows Phone ToProperty Identifies the To dependency property.

Top

Remarks

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 or 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, you associate an animation with an object's property value.

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 can be used together or separately to determine an animation's target values.

Properties specified

Resulting behavior

From

The animation progresses from the value specified by the From property to the base value of the property being animated.

From and To

The animation progresses from the value specified by the From property to the value specified by the To property.

From and By

The animation progresses from the value specified by the From property to the value specified by the sum of the From and By properties.

To

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.

By

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.

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 to animate between more than two target values, use a ColorAnimationUsingKeyFrames object.

Examples

The following example shows how to use ColorAnimation to animate the background color of a StackPanel.

Run this sample

<StackPanel x:Name="myStackPanel" Background="Red"
  Loaded="Start_Animation">
  <StackPanel.Resources>
    <Storyboard x:Name="colorStoryboard">

      <!-- Animate the background color of the canvas from red to green
        over 4 seconds. -->
      <ColorAnimation BeginTime="00:00:00" Storyboard.TargetName="myStackPanel" 
        Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
        From="Red" To="Green" Duration="0:0:4" />

    </Storyboard>
  </StackPanel.Resources>
</StackPanel>
' Start the animation when the object loads
Private Sub Start_Animation(ByVal sender As Object, ByVal e As EventArgs)
    colorStoryboard.Begin()
End Sub
// Start the animation when the object loads.
private void Start_Animation(object sender, EventArgs e)
{
    colorStoryboard.Begin();
}

Notice in the example above that the property value being animated (Color), belongs to a SolidColorBrush object which is not named or even explicitly declared. This indirect targeting is accomplished using the special syntax below.

Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"

Alternatively, you could explicitly create the SolidColorBrush, name it, and target its Color property directly. The example below shows how to create the same animation as the previous one except it uses direct property targeting.

<StackPanel Loaded="Start_Animation">
  <StackPanel.Resources>
    <Storyboard x:Name="colorStoryboard">
      <!-- Animate the background color of the canvas from red to green
        over 4 seconds. -->
      <ColorAnimation BeginTime="00:00:00" Storyboard.TargetName="mySolidColorBrush"
        Storyboard.TargetProperty="Color" From="Red" To="Green" Duration="0:0:4" />
    </Storyboard>
  </StackPanel.Resources>

  <StackPanel.Background>
    <SolidColorBrush x:Name="mySolidColorBrush" Color="Red" />
  </StackPanel.Background>

</StackPanel>
' Start the animation when the object loads
Private Sub Start_Animation(ByVal sender As Object, ByVal e As EventArgs)
    colorStoryboard.Begin()
End Sub
// Start the animation when the object loads.
private void Start_Animation(object sender, EventArgs e)
{
    colorStoryboard.Begin();
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.