Share via


ColorAnimationUsingKeyFrames Object

Animates the value of a Color property along a set of KeyFrames over a specified Duration.

XAML
<ColorAnimationUsingKeyFrames>
  oneOrMoreColorKeyFrames
</ColorAnimationUsingKeyFrames>
Scripting
To create an object using scripting, see CreateFromXAML.

XAML Values

oneOrMoreColorKeyFrames One or more ColorKeyFrame object elements that define the key frames for the animation. These can be any mixture of <LinearColorKeyFrame .../>, <DiscreteColorKeyFrame .../>, and <SplineColorKeyFrame .../>. Object elements defined here become members of the collection held by the KeyFrames property, when accessed by scripting at runtime.

Properties

AutoReverse, BeginTime, Duration, FillBehavior, KeyFrames, Name, RepeatBehavior, SpeedRatio, Storyboard.TargetName, Storyboard.TargetProperty

Methods

Equals, FindName, GetHost, GetValue, SetValue

Remarks

A key frame animation's target values are defined by its KeyFrames property, which contains a collection of ColorKeyFrame objects. Each ColorKeyFrame defines a segment of the animation with its own target Value and KeyTime. When the animation runs, it progresses from one key value to the next at the specified key times.

Unlike a ColorAnimation, a ColorAnimationUsingKeyFrames can have more than two target values; each keyframe can potentially represent a different value as the animation progresses on the overall timeline. You can also control the interpolation method of individual ColorKeyFrame segments. There are three types of ColorKeyFrame classes, one for each supported interpolation method: LinearColorKeyFrame, DiscreteColorKeyFrame, and SplineColorKeyFrame.

When declaring a ColorAnimationUsingKeyFrames in XAML, the order of the ColorKeyFrame object elements is not significant, because the KeyTime controls the timing and therefore the order in which the key frames are executed. However, it is good markup style to keep the element order the same as the KeyTime sequence order.

Examples

The following example uses the ColorAnimationUsingKeyFrames class to animate the Background property of a Canvas. This animation uses three key frames in the following manner:

  1. During the first two seconds, LinearColorKeyFrame gradually changes the color from green to red. Linear key frames like LinearColorKeyFrame create a smooth linear transition between values.
  2. During the end of the next half second, DiscreteColorKeyFrame quickly changes the color from red to yellow. Discrete key frames like DiscreteColorKeyFrame create sudden changes between values, the animation occurs quickly and has no interpolation between values at all.
  3. During the final two seconds, SplineColorKeyFrame changes the color again, this time from yellow back to green. Spline key frames like SplineColorKeyFrame create a variable transition between values according to the values of the KeySpline property. A KeySpline provides a way to alter the relationship of time versus value during the animation duration to be nonlinear, and in particular the relationship can be a curve that would be difficult to produce with individual keyframes. In this example, the change in color begins slowly and speeds up exponentially toward the end of the time segment.
XAML
<Canvas
  xmlns="https://schemas.microsoft.com/client/2007"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  Width="200" Height="200"
  Background="White"
  x:Name="Page">
  <Canvas.Triggers>
    <EventTrigger RoutedEvent="Canvas.Loaded">
      <BeginStoryboard>
        <Storyboard x:Name="ColorStoryboard">
          <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Page" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
          
            <!-- Go from green to red in the first 2 seconds. LinearColorKeyFrame creates
            a smooth, linear animation between values. -->
            <LinearColorKeyFrame Value="Red" KeyTime="00:00:02" />
            <!-- In the next half second, go to yellow. DiscreteColorKeyFrame creates a 
            sudden jump between values. -->
            <DiscreteColorKeyFrame Value="Yellow" KeyTime="00:00:2.5" />
            <!-- In the final 2 seconds of the animation, go from yellow back to green. SplineColorKeyFrame 
            creates a variable transition between values depending on the KeySpline property. In this example,
            the animation starts off slow but toward the end of the time segment, it speeds up exponentially.-->
            <SplineColorKeyFrame Value="Green" KeyTime="00:00:4.5" KeySpline="0.6,0.0 0.9,0.00" />
          </ColorAnimationUsingKeyFrames>
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Canvas.Triggers>
</Canvas>

See Also

Key-Frame Animations Overview
Animation Overview
Interactive Animations Overview