Create a new event handler method
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.
Note
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.
To create a new event handler method
Open your project in Expression Blend.
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.
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.
In the Properties panel, click the Events button.A list of all available events for the selected element appears, in alphabetical order.
Warning
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).
Tip
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.
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.
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.
Tip
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.
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) { } }
Note
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 ExploreProject 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.
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).
Note
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.
Note
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.