Export (0) Print
Expand All

Create a new event handler method

This page applies to WPF and Silverlight 2

When working with a Windows Presentation Foundation or Silverlight 2 project, Microsoft Expression Blend can generate code for empty event handler methods, to help you get started.

Cc294821.alert_note(en-us,Expression.10).gifNote:

For information about Microsoft Silverlight 1.0 event handling, see Overview of Silverlight 1.0 and Create a button that controls a storyboard in a Silverlight application. Also see Silverlight scripting and mouse events.

If you have Microsoft Visual Studio 2008 Standard Edition or later installed, Expression Blend will open your project in Visual Studio 2008, open your code-behind file, and then paste in the empty event handler method. If you do not have Visual Studio 2008 Standard Edition or later installed, Expression Blend will copy the empty event handler method to the Clipboard so that you can manually open your code-behind file and paste the code in.

For more information about event handlers in code, see Event handling and user interactivity in this User Guide. For information about creating interactivity in your Expression Blend application without using code, see Triggers and user interactivity in this User Guide.

  1. Open your project in Expression Blend.

  2. Open your document (for example, Window1.xaml) by double-clicking it under Files in the Project panel.Your document will open for editing. Make sure you are in Design view, by clicking the Design tab on the right side of the artboard.

  3. Under Objects and Timeline in the Interaction panel, select the element that you want to hook up to an existing event handler method. For example, if you want a rectangle element to move when a button is clicked, select the button element.The background behind the name of the element is highlighted, to show that the element is selected.

  4. In the Properties panel, click the Events Cc294821.6c67bb3b-e8a2-4a63-bad5-54d5c15b04dd(en-us,Expression.10).png button.A list of all available events for the selected element appears, in alphabetical order.

    Cc294821.alert_caution(en-us,Expression.10).gifCaution:

    Events will not appear if the project is still in a temporary state. For example, a newly-created project is stored in a temporary location until you click File and Save All. After you save the project, you will be able to see and select events in the Events panel.Additionally, events will not appear if you have more than one object selected at the same time under Objects and Timeline (for example, by holding the CTRL key to select multiple objects).

    Cc294821.alert_tip(en-us,Expression.10).gifTip:

    To see a short description of an event, move your mouse pointer over the name of the event. A tooltip will appear, with a description of the event. You can also see a list of the available events and their descriptions in the Events quick reference.

  5. Locate the event that you want to add programming logic to. For example, in the rectangle and button example, you would locate the MouseDown event.

  6. There are two ways that you can generate the empty event handler method:

    • Double-click in the text box beside the event name. Expression Blend will generate a default name for your event handler method and enter it into the text box, and will generate the code for the empty method.

    • Type a name into the text box beside the event name, and then press ENTER or click somewhere else to move focus away from the text box. Event method names must begin with a letter. If the method name does not already exist in the code-behind file, Expression Blend will generate the code for the empty method and will use the name that you typed in.

    Cc294821.alert_tip(en-us,Expression.10).gifTip:

    You can configure Expression Blend to use the Clipboard, even if Visual Studio 2008 is installed. On the Tools menu, click Options, and then click Event Handlers. Under Editing experience, click Clipboard only.

  7. From this point, Expression Blend will do one of the following things:

    • If you have Visual Studio 2008 Standard Edition or later installed, Expression Blend will open your project in Visual Studio 2008, will open your code-behind file, and then will paste in the empty event handler method for you.

    • If you do not have Visual Studio 2008 Standard Edition or later installed, Expression Blend will copy the empty event handler method to the Clipboard and will display a pop-up window that describes what you can do next. In this case, you can open the code-behind file manually to paste the method inside the class definition for the window, as follows:

      public partial class Window1
      {
          public Window1()
          {
              this.InitializeComponent();
    
              // Insert code required on object creation below this point.
          }
    
          private void Button_MouseDown(object sender, RoutedEventArgs e)
          {
    
          }
      }
    
    Cc294821.alert_note(en-us,Expression.10).gifNote:

    If you have an application (such as Notepad) mapped to .cs or .vb files, you can open your code-behind file manually by double-clicking the file name under Files in the Project panel. Expression Blend will open the file in that application.

    If you do not have an application mapped to .cs or .vb files, Expression Blend will be unable to open the file externally. To open the file, locate it in Windows Explorer (or, if you have saved your project in Expression Blend, click Explore Project on the Project menu), right-click the .cs or .vb file that you want to edit, select Open With, and then select Notepad (or another editing application). After you do this, you will be able to open code-behind files manually from the Project panel in Expression Blend.

  8. With your code-behind file open and the event handler method pasted in, you can begin to add code to your method. For the purposes of this procedure, you could add the following line of code in red to make a message box appear when the button is clicked:

      private void Button_MouseDown(object sender, RoutedEventArgs e)
      {
          MessageBox.Show("Hello!");
      }
    

    For examples of event handler methods in code-behind files, see the samples that are available from the Welcome Screen (click Welcome Screen on the Help menu).

    Cc294821.alert_note(en-us,Expression.10).gifNote:

    Expression Blend will give you a build error if you reference an event handler method in the Events panel that does not exist in the code-behind file. If you get this kind of error, you can determine if there is a spelling mistake in the event handler method name, or you can double-click in the text box for the event to create a new empty event handler method.

    When you delete or rename an event handler method name in a text box in the Events panel, Expression Blend does not delete the original method from the code-behind file. This is because the event might still be used elsewhere. You will not receive a build error if there is an event handler method defined in your code-behind file that is not referenced in your XAML file.

    Cc294821.alert_note(en-us,Expression.10).gifNote:

    If you want to reference an element in your .xaml file from a code-behind file, you must name the element in the .xaml file. By default, elements that you create in Expression Blend are not named. You can name an element under Objects and Timeline in the Interaction panel by right-clicking the element and then clicking Rename.

Community Additions

ADD
Show:
© 2014 Microsoft