Content Index Search
 

Add animation to a button

The SimpleButton does not animate the changes in its state. Instead, it sets a brush resource when there are state changes. In Microsoft® Expression Blend™, you cannot 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 on the artboard of Expression Blend.
    Tip The simple style controls are available from the Simple Styles category of the Controls tab in the Asset Library Asset Library button. After you select a simple style control from the list, you can draw it on the artboard.
  2. Right-click the button under Objects and Timeline, point to Edit Control Parts (Template), and then click Edit Template. If you do not want to change the SimpleStyles.xaml resource dictionary, you can click Edit a Copy instead of Edit Template, 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 the template-editing mode and return to the scope of your document: click the Scope up button Scope up button, which is above the element tree in the Interaction panel.

    To return to template editing mode for an existing template: under Objects and Timeline, right-click the element whose template you want to edit, point to Edit Control Parts (Template), and then click Edit Template.
  3. In the editing scope of the control template, delete the Border child element of the Grid.
  4. Double-click the Grid element to make it active, so that you can add child elements.
  5. Draw a Rectangle Rectangle tool into the grid.
  6. Right-click the Rectangle element, point to Order, and then click Send to Back to layer it behind the ContentPresenter element.
  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 element 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. Template binding lets you use in your template some of the properties that are set on the control, and thus lets you create multiple buttons that have different background colors but still use the same template. However, you can not animate from a template bound property to a specific color, which is why we set a default color for the Fill property in the template.
  8. Under Triggers in the Interaction panel, click the IsMouseOver = True trigger to activate trigger recording. Then, beside Actions when activating, click the Add new action Add new action button button to add an animation timeline. If you do not already have a timeline that you want to trigger when the mouse 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 of the Interaction panel to see all of the triggers and actions under Triggers. Use your mouse to resize the windows.
  9. Under Objects and Timeline, move the playhead Animation 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 Keyframe indicator 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 the mouse pointer moves over the button.
  10. Under Triggers in the Interaction panel, click the IsMouseOver = True trigger again to exit the animation timeline and return to the recording mode for the trigger. Click the Add new action Add new action button button beside Actions when deactivating. Create or select the animation timeline that will run when the mouse 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 under Triggers.
  12. Test your application (F5) to see the effects.