How to: Animate Color by Using Key Frames

This example shows how to animate the Color of a SolidColorBrush by using key frames.


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

  1. During the first two seconds, uses an instance of the LinearColorKeyFrame class to gradually change 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, uses an instance of the DiscreteColorKeyFrame class to quickly change the color from red to yellow. Discrete key frames like DiscreteColorKeyFrame create sudden changes between values, that is, the color change in this part of the animation occurs quickly and is not subtle.

  3. During the final two seconds, uses an instance of the SplineColorKeyFrame class to change 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. In this example, the change in color begins slowly and speeds up exponentially toward the end of the time segment.

<Page  xmlns=""
  Title="ThicknessAnimationUsingKeyFrames Example">

  <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
    <Border Background="#99FFFFFF"  BorderThickness="28"
    Margin="0,60,0,20" Padding="20" >
        <SolidColorBrush x:Name="MyAnimatedBrush" Color="Green" />
        <EventTrigger RoutedEvent="Border.Loaded">

              <!-- Animate from green to red using a linear key frame, from red to 
              yellow using a discrete key frame, and from yellow back to green with
              a spline key frame. This animation repeats forever. -->
                Duration="0:0:6" FillBehavior="HoldEnd" RepeatBehavior="Forever">

                  <!-- Go from green to red in the first 2 seconds. LinearColorKeyFrame creates
                  a smooth, linear animation between values. -->
                  <LinearColorKeyFrame Value="Red" KeyTime="0:0:2" />

                  <!-- In the next half second, go to yellow. DiscreteColorKeyFrame creates a 
                  sudden jump between values. -->
                  <DiscreteColorKeyFrame Value="Yellow" KeyTime="0:0: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="0:0:4.5" KeySpline="0.6,0.0 0.9,0.00" />

        This example shows how to use the ColorAnimationUsingKeyFrames to create
        an animation on the BorderBrush property of a Border.


For the complete sample, see KeyFrame Animation Sample.

See Also

Community Additions