DoubleAnimation Object

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

XAML
<DoubleAnimation .../>
Scripting
To create an object using scripting, see the CreateFromXAML method.

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 DoubleAnimation class creates a transition between two target values. To set the target values, use the object's 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

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.

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

Examples

The following example shows how to use DoubleAnimation to create a rectangle that fades in and out of view after it is loaded.

XAML
<Canvas
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Rectangle
    x:Name="MyAnimatedRectangle"
    Width="100"
    Height="100"
    Fill="Blue">
    <Rectangle.Triggers>

      <!-- Animates the rectangle's opacity. -->
      <EventTrigger RoutedEvent="Rectangle.Loaded">
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation
              Storyboard.TargetName="MyAnimatedRectangle"
              Storyboard.TargetProperty="Opacity"
              From="1.0" To="0.0" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
    </Rectangle.Triggers>
  </Rectangle>
</Canvas>

The following example makes a rectangle fade from view when the user presses the left mouse button over it. Unlike the previous example, the animation does not start automatically.

XAML
<Canvas
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Canvas.Resources>
    <Storyboard x:Name="myStoryboard">
      <DoubleAnimation
        Storyboard.TargetName="MyAnimatedRectangle"
        Storyboard.TargetProperty="Opacity"
        From="1.0" To="0.0" Duration="0:0:5" 
        AutoReverse="True" />
    </Storyboard>  
  </Canvas.Resources>
  <Rectangle
    x:Name="MyAnimatedRectangle"
    Width="100"
    Height="100"
    Fill="Blue"
    MouseLeftButtonDown="startAnimation">
  </Rectangle>
</Canvas>
JavaScript
function startAnimation(sender, mouseEventArgs)
{
    // Retrieve the Storyboard and begin it.
    sender.findName("myStoryboard").begin();

}

See Also

Silverlight Key-Frame Animations Overview
Interactive Animations Overview

Community Additions

Show: