The Storyboard Class
The storyboard manages the timeline of your animation. You can use a storyboard to group multiple animations, and it also has the ability to control the playback of animation -- pausing it, stopping it, and changing its position. However, the most basic feature provided by the Storyboard class is its ability to point to a specific property and specific element using the TargetProperty and TargetName properties. In other words, the storyboard bridges the gap between your animation and the property you want to animate.
Here's how you might define a storyboard that applies a DoubleAnimation to the Width property of an element named rect:
Storyboard x:Name="storyboard" Storyboard.TargetName="rect" Storyboard.TargetProperty="Width"> <DoubleAnimation From="160" To="300" Duration="0:0:5">:<:/DoubleAnimation> </Storyboard>
Both TargetName and TargetProperty are attached properties. That means you can apply them directly to the animation, as shown here:
Storyboard x:Name="storyboard"> <DoubleAnimation Storyboard.TargetName="rect" Storyboard.TargetProperty="Width" From="160" To="300" Duration="0:0:5"></DoubleAnimation> </Storyboard>
This syntax is more common, because it allows you to put several animations in the same storyboard but allow each animation to act on a different element and property.
If you give your storyboard a name (as in the previous example), you can interact with it in code. The Storyboard class includes four methods that let you manually control the animation: Begin(), Stop(), Pause(), and Resume(). However, if you simply want to start your animation in response to some event, there's an easier solution. You can wire this event directly to the BeginStoryboard action, as described in the next section.
The Event Trigger
Defining a storyboard and an animation are the first steps to creating an animation. To actually put this storyboard into action, you need an event trigger. An event trigger responds to an event by performing a storyboard action. The only storyboard action that Silverlight currently supports is BeginStoryboard, which starts a storyboard (and hence all the animations it contains).
The following example uses the Triggers collection of a Canvas to attach an animation to the Loaded event. When the Silverlight content is first rendered in the browser, and the Canvas element is loaded, the rectangle begins to grow. Five seconds later, its width has stretched from 160 pixels to 300.
<Canvas ... > <Canvas.Triggers> <EventTrigger RoutedEvent="Canvas.Loaded"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="rect" Storyboard.TargetProperty="Width" From="160" To="300" Duration="0:0:5"></DoubleAnimation> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Canvas.Triggers> <Rectangle Name="rect" Height="40" Width="160" Fill="Blue" Canvas.Left="10" Canvas.Top="10"></Rectangle> </Canvas>
The Storyboard.TargetProperty property identifies the property you want to change (in this case, Width). If you don't supply a class name, the storyboard uses the parent element. If you want to set an attached property (for example, Canvas.Left or Canvas.Top), you need to wrap the entire property in brackets, like this:
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" ... />
If you want to use multiple animations in the same storyboard, you simply need to add more than one <Animation> element inside the <Storyboard> element. For example, you could use this technique to make the rectangle grow in width and height at the same time.