Try it: Add animation to a button

This page applies to WPF projects only

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

  1. 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.

  2. 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 toCc294783.55844eb3-ed98-4f20-aa66-a6f5b23eeb2b(en-us,Expression.30).png, 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.

  3. In the editing scope of the control template, delete the Border child object of the Grid.

  4. Double-click the Grid object to make it active, so that you can add child objects.

  5. Draw a Rectangle Cc294783.ae750268-92e8-403a-9e07-b662da4e9e1e(en-us,Expression.30).png in the grid.

  6. Right-click the Rectangle object, point to Order, and then click Send to Back to layer it behind the ContentPresenter object.

  7. 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.

  8. In the Triggers panel, click the IsMouseOver = True trigger to activate trigger recording. Then, next to Actions when activating, click Add new actionCc294783.d02551d0-d56a-4ca3-9d5f-58fbc6bda0c3(en-us,Expression.30).png 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.

  9. In the Objects and Timeline panel, move the playhead Cc294783.5626c9eb-40bb-450a-9ca1-3678e5abe429(en-us,Expression.30).png 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 Cc294783.fa3c696d-5463-4000-8a6b-650fe6759bf7(en-us,Expression.30).png 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.

  10. In the Triggers panel, click the IsMouseOver = True trigger again to exit the animation timeline and return to the recording mode for the trigger. Click Add new action Cc294783.d02551d0-d56a-4ca3-9d5f-58fbc6bda0c3(en-us,Expression.30).png 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.

  11. If you like, you can create animation timelines for other states in the Triggers panel.

  12. Test your application (F5) to see the effects.

See also

Tasks

Add or remove a trigger

Create a simple animation

Concepts

SimpleButton

Using triggers to define the behavior of a WPF control