Create a button that controls a storyboard in a Silverlight application

This page applies to Silverlight 1 projects only

Creating a button in a Microsoft Silverlight 1.0 application involves drawing the elements that make up the button, wrapping them in a layout panel for easy copying, and writing JavaScript code that will perform some action when button events occur (such as when someone clicks your button).

Note

For a complete sample using multiple button events, see the ButtonGallery sample, which you can open by clicking Welcome Screen on the Help menu, and then clicking ButtonGallery on the Samples tab.

Draw the elements of your button

You can use any elements to create the look of your button, including shapes, lines, text, and images. The following procedure shows you how to draw a rectangle and a text block, but you could use any one of the procedures listed in the Add elements to a XAML document in a Silverlight project topic.

To draw the elements of your button

  1. With a Silverlight 1.0 project open in Microsoft Expression Blend 2, select the Rectangle tool from the Toolbox.

    Cc295092.ddce7e9a-415d-417c-a4e6-ad1ae921b35c(en-us,Expression.10).png

    If you cannot see the Rectangle tool, right-click the tool containers to locate and select the Rectangle tool.

    Cc295092.8fbbbb21-be83-4cf6-903b-3a49f00c9860(en-us,Expression.10).png

  2. Draw a rectangle on the artboard by dragging your pointer. As you draw, the pointer will change to a crosshair Cc295092.ae0eb9fb-170a-44f8-a9ef-cf7e2b7313cc(en-us,Expression.10).png.

    Cc295092.4da470fa-7261-468c-828a-6694f55af53b(en-us,Expression.10).png

  3. With the new rectangle element selected under Objects and Timeline, change its appearance. For example, under Brushes in the Properties panel, click Fill on the Gradient brush Cc295092.91fb0c61-7b3b-4ae7-8a59-760e625e3bd7(en-us,Expression.10).png tab, and then define colors for the gradient stops.

    Cc295092.d0da2125-740c-4650-945d-c19e513be482(en-us,Expression.10).png

    Note

    You can make more changes to the appearance of the rectangle by using the procedures in Appearance and Transformations.

  4. Select the TextBlock control from the Toolbox.

    Cc295092.40e445b9-f7e7-407e-bda6-0dc6225a5a1a(en-us,Expression.10).png

  5. Draw a text block element on top of the rectangle. You might have to resize the rectangle or the text block elements or move them around by selecting the Selection tool Cc295092.2ff91340-477e-4efa-a0f7-af20851e4daa(en-us,Expression.10).png from the Toolbox and dragging the blue adorners around the elements on the artboard.

    Cc295092.8737c6dd-140f-4894-b25b-74038a1e848d(en-us,Expression.10).png

  6. With the new text block element selected under Objects and Timeline, press F2 to enter text-editing mode, and type Button to change the text that is displayed in the element. Click outside the text block element to exit text-editing mode.

    Note

    For more information about how to modify the appearance of your text block, see the topics under Text and typography.

  7. To finish the look of your button, you can draw more elements on the artboard using the procedures listed in Add elements to a XAML document in a Silverlight project.

Cc295092.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

Wrap your button elements in a layout panel

By wrapping your button elements in a single layout panel (a Canvas element), you simplify the process of duplicating your button. This is because you can copy and paste the layout panel instead of selecting all the elements that make up your button and then duplicating them. Additionally, wrapping your button elements in the Canvas layout panel also makes it simpler to create event handlers for your buttons. For example, you can create an event handler that responds when the pointer moves across the layout panel as a whole instead of responding when the pointer moves across a part of the button.

To wrap elements in a layout panel

  1. Under Objects and Timeline, hold the CTRL key when you select all the elements that make up your button.

  2. Right-click your selected elements, point to Group Into, and then click Canvas.

    Cc295092.aa25e7d8-ebf1-4b0b-a8d9-e6e0353a57ab(en-us,Expression.10).png

  3. Right-click the new canvas element under Objects and Timeline, click Rename, enter a new name for your button (for example MyButton), and then press ENTER.

    Now, when you want to duplicate your button, you can copy and paste the MyButton element.

Cc295092.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

Create an animation storyboard to change the appearance of your button

Typically, when users click a button in an application, the button shows a visible change. The following procedure shows you how to create an animation storyboard that makes your button elements expand.

Note

You could animate any elements in your application, not just the elements that belong to the button. For example, you could have a small image that would circle around your button when it is clicked.

To create the animation storyboard

  1. Under Objects and Timeline, click the New button Cc295092.86937695-03dd-44ea-aa30-28d4029b3ad0(en-us,Expression.10).png.

    Cc295092.d32fad7c-41a1-4313-8a77-ace5b7c1108a(en-us,Expression.10).png

    The Create Storyboard dialog box opens.

    Cc295092.68230d0a-c687-4ca3-af6a-63e7f2e7a9a7(en-us,Expression.10).png

  2. By default, the Name of your storyboard is Storyboard1. Keep the name as Storyboard1 because the code in the next procedure refers to that name.

  3. Also by default, the Create as Storyboard check box is selected. Do not clear the check box because you do not want the animation to run immediately when the application is loaded in the browser window. Instead, you want the animation to be completely controlled (started and stopped) by code.

  4. Click OK.

    Expression Blend creates the storyboard and enters animation mode.

    Cc295092.c541e913-6fe4-4b2b-9ae1-a275a74d080b(en-us,Expression.10).png

    Note

    You could optionally change the location of the Interaction panel by pressing F6. When the Interaction panel is under the artboard, you can view more of the timeline.

  5. Under Objects and Timeline, select the MyButton element.

  6. Above the timeline, click the Record Keyframe button Cc295092.de094a9c-58f2-4466-912a-9bdc18362548(en-us,Expression.10).png.

    A new keyframe is set to record the current appearance of the button elements.

  7. Move the timeline playhead Cc295092.5626c9eb-40bb-450a-9ca1-3678e5abe429(en-us,Expression.10).png to the half-second mark (the animation's ending time) and click the Record Keyframe button Cc295092.de094a9c-58f2-4466-912a-9bdc18362548(en-us,Expression.10).png again.

    A new keyframe is set to record the current appearance of the button elements. The button will resemble this at the end of the animation.

    Cc295092.a797d02b-40fd-4617-8493-f9702578a125(en-us,Expression.10).png

  8. Move the timeline playhead Cc295092.5626c9eb-40bb-450a-9ca1-3678e5abe429(en-us,Expression.10).png to the quarter-second mark. This is where you will create a change in the appearance of your button.

    Note

    If you cannot move the timeline to exactly the quarter-second mark (0:00.250), it is because the snap resolution is too low. To increase the snap resolution, click the Snapping options button Cc295092.12545ddf-9f78-43f0-ac71-578cda69f8c1(en-us,Expression.10).png, set the Snap resolution per second to 20, and then click OK.

  9. With the timeline playhead at the quarter-second mark and the MyButton element selected, change the size of your button on the artboard. You can do this by dragging the bottom-right resizing handle when holding the ALT key to maintain the center point.

    Cc295092.851d9480-57da-461f-b8d2-748f898f56b1(en-us,Expression.10).png

    New keyframes are automatically added to the timeline at the quarter-second mark to record the new size and position of your button. Notice that a RenderTransform element has been added under Objects and Timeline to specify the new size (Scale) and new position (Translation) of your button, and that keyframes for these elements have been added where you had set keyframes on MyButton earlier. Expression Blend assumes that you want to animate from the starting and ending keyframes that were set on MyButton. Therefore, it sets starting and ending keyframes on the RenderTransform element for you.

  10. Click the Play Cc295092.64ad8e84-1eec-4154-9d0c-11fef322c0bf(en-us,Expression.10).png button to test your animation.

  11. Exit animation mode by clicking the Close Storyboard button Cc295092.4c066464-3a41-452d-b2e9-e95f6c5659ff(en-us,Expression.10).png. This makes sure that any changes that you make to your elements now are not a part of the animation storyboard.

  12. Save all your files.

Cc295092.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

Create an event handler to trigger the animation storyboard when your button is clicked

The work of registering an event handler and writing the code for the event handler can all be done in the code-behind file. This enables programmers and designers to work separately, easily combining the presentation of an application and the code when both are finished. Additionally, a programmer could cause other actions to occur when a button is clicked by adding code to the event handler, such as navigating to a web page. For an example, see Create a hyperlink in a Silverlight application.

To create the event handler

  1. In the Project tab, double-click the code-behind file, Page.xaml.js, to open it for editing.

    The Page.xaml.js file already contains the following JavaScript code:

    • A definition for the Page object.

    • Registration for the MouseLeftButtonDown event.

    • A definition of the event handler for the MouseLeftButtonDown event.

    if (!window.UntitledProject10)
        UntitledProject10 = {};
    
    UntitledProject10.Page = function()
    {
    }
    
    UntitledProject10.Page.prototype =
    {
        handleLoad: function(control, userContext, rootElement)
        {
            this.control = control;
    
            // Sample event hookup:
            rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
        },
    
        // Sample event handler
        handleMouseDown: function(sender, eventArgs)
        {
            // The following line of code shows how to find an element by name and call a method on it.
            // this.control.content.findName("Storyboard1").Begin();
        }
    }
    

    Note

    For information about JavaScript in Microsoft Silverlight and the structure of objects, see Scripting and mouse events.

  2. The code to call your storyboard when the left mouse button is clicked is already present, but commented out using double slashes (//).

                    // this.control.content.findName("Storyboard1").Begin();
    

    Remove the double slashes to uncomment the code.

    this.control.content.findName("Storyboard1").Begin();
    
  3. Save all your files.

  4. Test your project by clicking Test Project on the Project menu, or by pressing F5.

    If the test build is successful, your project will automatically open in your default web browser by using the local development server that is included with Microsoft Expression Studio. For more information about how to test Silverlight projects, see Test a Silverlight project from Expression Blend.

    Click your button to see whether your storyboard is triggered. Notice that clicking anywhere inside the browser also triggers your storyboard. This is because the event handler was registered in the scope of the root element (Page).

  5. Comment out the line of code where the event handler is registered by adding double slashes in front of the line so that it resembles the following:

                    //rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
    
  6. Add the following line of code after the line that was commented out:

                    this.control.content.findName("MyButton").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
    

    Notice that the only difference between these two lines of code is the element on which the event handler is registered. In the line that you commented out, the event handler was registered on the rootElement. In the line that you pasted in, the event handler was registered on the MyButton element by using the findName method to locate the element.

  7. Save all your files again.

  8. Test your project by clicking Test Project on the Project menu, or by pressing F5.

    Click your button, and click away from your button, to confirm that the animation storyboard is triggered only when you click your button.

Cc295092.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

See also

Concepts

Test a Silverlight project from Expression Blend