Compartir a través de


ColorAnimation

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

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

<ColorAnimation   .../>

Managed Equivalent

ColorAnimation

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 object creates a transition between two target values. To set its target values, use its From, To, and By properties. The following table summarizes how you can use the From, To, and By properties together or separately to determine an animation's target values.

Properties specified

Resulting behavior

From

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

From and To

The animation progresses from the value of the From property to the value of the To property.

From and By

The animation progresses from the value of 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 of 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.

For more information on basic concepts, see Animation Overview. Note that the Animation Overview topic is written primarily for users of the managed API, and may not have code examples or specific information that address the JavaScript API scenarios.

Example

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

<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  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">

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

        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Canvas.Triggers>
</Canvas>

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

<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  Width="200" Height="200">
  <Canvas.Background>
    <SolidColorBrush x:Name="mySolidColorBrush" Color="White" />
  </Canvas.Background>
  <Canvas.Triggers>
    <EventTrigger RoutedEvent="Canvas.Loaded">
      <BeginStoryboard>
        <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>
      </BeginStoryboard>
    </EventTrigger>
  </Canvas.Triggers>
</Canvas>

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

<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  Width="200" Height="200">
  <Canvas.Background>
    <SolidColorBrush x:Name="mySolidColorBrush" Color="White" />
  </Canvas.Background>
  <Canvas.Triggers>
    <EventTrigger RoutedEvent="Canvas.Loaded">
      <BeginStoryboard>
        <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>
      </BeginStoryboard>
    </EventTrigger>
  </Canvas.Triggers>
</Canvas>