Silverlight Tutorial - Color Animations
Storyboard and ColorAnimation XAML tags to create custom color animations. Color animations are commonly used on input controls like buttons to give visual feedback that the cursor is over the control or when the button has been pressed. This tutorial will show a couple of simple examples that can easily be extended to complex controls and animations.
| The examples in this post were built using Visual Studio 2008 beta 2, Silverlight 1.1 Alpha Refresh, and C#. For information on where to download these, please visit our previous tutorial Getting Started With Silverlight. |
Let's start with a very simple example. Here, we're going to change the color of a rectangle when the mouse enters and leaves it.
We'll start by defining the XAML for the rectangle.
<Rectangle
x:Name='myRectangle'
Fill='#FF0000'
Width='120' Height='44'
MouseEnter='MyRectangleMouseEnter'
MouseLeave='MyRectangleMouseLeave' />
x:Name='myRectangle'
Fill='#FF0000'
Width='120' Height='44'
MouseEnter='MyRectangleMouseEnter'
MouseLeave='MyRectangleMouseLeave' />
Here we've defined a very simple red rectangle. Since we want the color to change when the user enters or leaves the control with the cursor, we've defined event handlers for each of these. The code inside these event handlers is located in the C# code behind and will be explained a little later.
Now that we have the rectangle defined, let's create an animation to change the color of the rectangle when the user enters it with the mouse cursor.
<Canvas.Resources>
<Storyboard x:Name="mouseEnter">
<ColorAnimation
Duration='00:00:01'
To='#000000'
Storyboard.TargetName='myRectangle'
Storyboard.TargetProperty=
'(Shape.Fill).(SolidColorBrush.Color)' />
</Storyboard>
</Canvas.Resources>
<Storyboard x:Name="mouseEnter">
<ColorAnimation
Duration='00:00:01'
To='#000000'
Storyboard.TargetName='myRectangle'
Storyboard.TargetProperty=
'(Shape.Fill).(SolidColorBrush.Color)' />
</Storyboard>
</Canvas.Resources>
The first thing you might notice here is the
<Canvas.Resources> tag. This tag holds a collection of Storyboards and is located directly after the opening Canvas tag. Inside this tag is where we'll be placing the Storyboards that will control our color animations.
The first Storyboard we're going to make will change the color of the rectangle when the user enters the control with the mouse. In order to control the Storyboard from the C# code-behind, you'll have to first give it a name. I named mine "mouseEnter". Next we add a
ColorAnimation inside the Storyboard. The first attribute we set is Duration, which is how long the animation will run - in this case, 1 second. Next, we set the end result of the animation. I want my rectangle to fade from red to black, so I set To to "#000000". The next thing we set is the Storyboard.TargetName, which is the name of the object we want to modify. This has to be set to the same thing that we named the rectangle, in this case "myRectangle". Storyboard.TargetProperty is the property the animation will be changing, which in this case is the fill color. It might seem like a complicated way to address the rectangle's fill color, but it's necessary. If we expanded out the rectangle to use a SolidColorBrush it might make a little more sense:
<Rectangle
x:Name='myRectangle'
Width='120' Height='44'
MouseEnter='MyRectangleMouseEnter'
MouseLeave='MyRectangleMouseLeave'>
<Rectangle.Fill>
<SolidColorBrush Color='#FF0000' />
</Rectangle.Fill>
</Rectangle>
x:Name='myRectangle'
Width='120' Height='44'
MouseEnter='MyRectangleMouseEnter'
MouseLeave='MyRectangleMouseLeave'>
<Rectangle.Fill>
<SolidColorBrush Color='#FF0000' />
</Rectangle.Fill>
</Rectangle>
So in the attribute
<Storyboard.TargetProperty>, "(Shape.Fill)" refers to <Rentagle.Fill> and "(SolidColorBrush.Color)" is the Color attribute of <SolidColorBrush>.
All right, the storyboard and animation are done and ready to be run. Let's create some C# code that will handle the events.
private void MyRectangleMouseEnter(object sender, EventArgs e)
{
this.mouseEnter.Begin();
}
{
this.mouseEnter.Begin();
}
If you remember, we added a
MouseEnter event to our XAML code and told it to execute the method MyRectangleMouseEnter. All this function does is to start the Storyboard we named "mouseEnter". If you build and preview your project, you should get something that looks like the following:
If you ask me, that's not very interesting. Let's add in the XAML code to make the rectangle fade back to red when the mouse leaves the control.
<Canvas.Resources>
<Storyboard x:Name="mouseEnter">
<ColorAnimation
Duration='00:00:01'
To='#000000'
Storyboard.TargetName='myRectangle'
Storyboard.TargetProperty=
'(Shape.Fill).(SolidColorBrush.Color)' />
</Storyboard>
<Storyboard x:Name='mouseLeave'>
<ColorAnimation
Duration='00:00:01'
To='#FF0000'
Storyboard.TargetName='myRectangle'
Storyboard.TargetProperty=
'(Shape.Fill).(SolidColorBrush.Color)' />
</Storyboard>
</Canvas.Resources>
<Storyboard x:Name="mouseEnter">
<ColorAnimation
Duration='00:00:01'
To='#000000'
Storyboard.TargetName='myRectangle'
Storyboard.TargetProperty=
'(Shape.Fill).(SolidColorBrush.Color)' />
</Storyboard>
<Storyboard x:Name='mouseLeave'>
<ColorAnimation
Duration='00:00:01'
To='#FF0000'
Storyboard.TargetName='myRectangle'
Storyboard.TargetProperty=
'(Shape.Fill).(SolidColorBrush.Color)' />
</Storyboard>
</Canvas.Resources>
To make the rectangle fade back to red, I simply added another
Storyboard to the canvas resources. The only thing different from the previous ColorAnimation is that To is now set to red (#FF0000), instead of black (#000000).
Just like with the previous storyboard, we'll going to need some C# code to run the animation. This time, the function is hooked up to the rectangle's
MouseLeave event.
private void MyRectangleMouseLeave(object sender, EventArgs e)
{
this.mouseLeave.Begin();
}
{
this.mouseLeave.Begin();
}
That's it. Now we have a rectangle that fades to black when the mouse enters it and fades back to red when the mouse leaves.
We've only barely touched the surface for the possibilities of what a color animation is capable of - but hopefully this tutorial will provide you with a launching point for your own complex (and much more interesting) animations.
Posted in Silverlight, XAML, All Tutorials by The Reddest |

September 19th, 2007 at 1:45 am
Nice! Just the input I needed
September 21st, 2007 at 9:23 am
What does the event handler look like in VB.NET?
October 31st, 2007 at 8:38 am
Cool effect.
Nice way to start with animations.
Thanks for the post.
January 19th, 2008 at 7:05 am
Superb! Simple for dummies like me to get to grips with and then build on. What a good tutorial should be.
August 14th, 2008 at 2:40 am
Hi
How can i change foreground of text on mouse enter.Can n e body help me
October 29th, 2008 at 2:20 pm
Nice simple way to introduce animations and how to trigger them from code behind events. Exactly what I was looking to do. Great work and thanks for sharing!