Try it: Add animation to a button
The SimpleButton style does not animate the changes in its state. Instead, it sets a brush resource when there are state changes. In Microsoft Expression Blend, you can't animate from a brush resource to a color. The following procedure shows you an alternative, by using property triggers to start animation timelines.
To animate a button
Draw a SimpleButton object on the artboard of Expression Blend.
Tip
The simple style controls are available under Simple Styles in the Styles category of the Assets panel. After you select a simple style control from the list, you can draw it on the artboard.
Right-click the button in the Objects and Timeline panel, point to Edit Template, and then click Edit Current. If you do not want to change the SimpleStyles.xaml resource dictionary, you can click Edit a Copy instead of Edit Current, to create a new template and save it in the document.
For more information about creating a copy, see Create a resource.
Tip
To exit template-editing mode and return to the scope of your document, click Return scope to, which is above the object tree in the Objects and Timeline panel.
To return to template-editing mode for an existing template, in the Objects and Timeline panel, right-click the object whose template you want to edit, point to Edit Template, and then click Edit Current.
In the editing scope of the control template, delete the Border child object of the Grid.
Double-click the Grid object to make it active, so that you can add child objects.
Draw a Rectangle in the grid.
Right-click the Rectangle object, point to Order, and then click Send to Back to layer it behind the ContentPresenter object.
Set a new value for the Fill property of the rectangle under Brushes in the Properties panel. This will be the default color of your button.
Tip
The Border child object of the original template used template binding to bind the Fill property of the template to the Background property of the control to which the template is applied. You can use template binding to carry object properties through to the template, and thus lets you create multiple buttons that have different background colors but still use the same template. However, you can't animate from a template-bound property to a specific color, which is why you set a default color for the Fill property in the template.
In the Triggers panel, click the IsMouseOver = True trigger to activate trigger recording. Then, next to Actions when activating, click Add new action to add an animation timeline. If you do not already have a timeline that you want to trigger when your pointer moves over the button, a Timeline Needed window will appear. Click OK to create a new timeline and to begin recording.
Tip
You might have to adjust the windows in the Triggers panel to see all of the triggers and actions. Use your mouse to resize the windows.
In the Objects and Timeline panel, move the playhead to 1 second, and then set a new value for the Fill property under Brushes in the Properties panel.
As soon as you change the Fill property, a keyframe appears on the timeline. You do not need to set a keyframe at 0 seconds. The timeline will animate from the previous fill to the fill that is set at 1 second, when your pointer moves over the button.
In the Triggers panel, click the IsMouseOver = True trigger again to eit the animation timeline and return to the recording mode for the trigger. Click Add new action beside Actions when deactivating. Create or select the animation timeline that will run when your pointer moves away from the button. For example, you could use the previous timeline, but select the Stop method for the timeline.
If you like, you can create animation timelines for other states in the Triggers panel.
Test your application (F5) to see the effects.
See also
Tasks
Add or remove a trigger from a WPF control
Create a simple animation
Concepts
SimpleButton
Using triggers to define the behavior of a WPF control
Send feedback about this topic to Microsoft. © 2011 Microsoft Corporation. All rights reserved.