|
| 1 | +--- |
| 2 | +title: "How to add an event handler using code" |
| 3 | +description: Learn how to add an event handler in code-behind for an element in Windows Presentation Foundation (WPF). |
| 4 | +ms.date: "02/01/2021" |
| 5 | +dev_langs: |
| 6 | + - "csharp" |
| 7 | + - "vb" |
| 8 | +helpviewer_keywords: |
| 9 | + - "event handlers [WPF], adding" |
| 10 | + - "XAML [WPF], adding event handlers" |
| 11 | +--- |
| 12 | +<!-- The acrolinx score was 99 on 02/01/2021--> |
| 13 | + |
| 14 | +# How to add an event handler using code (WPF .NET) |
| 15 | + |
| 16 | +You can assign an event handler to an element in Windows Presentation Foundation (WPF) using markup or code-behind. Although it's customary to assign an event handler in Extensible Application Markup Language (XAML), sometimes you might have to assign an event handler in code-behind. For instance, use code when: |
| 17 | + |
| 18 | +- You assign an event handler to an element after the markup page that contains the element loads. |
| 19 | +- You add an element and assign its event handler after the markup page that will contain the element loads. |
| 20 | +- You define the element tree for your application entirely in code. |
| 21 | + |
| 22 | +[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)] |
| 23 | + |
| 24 | +## Syntax for event handler assignment |
| 25 | + |
| 26 | +C# supports event handler assignment using: |
| 27 | + |
| 28 | +- The `+=` operator, which is also used in the common language runtime (CLR) event handling model. |
| 29 | +- The <xref:System.Windows.UIElement.AddHandler%2A?displayProperty=nameWithType> method. |
| 30 | + |
| 31 | +VB supports event handler assignment using: |
| 32 | + |
| 33 | +- The [AddHandler](/dotnet/visual-basic/language-reference/statements/addhandler-statement) statement with the [AddressOf](/dotnet/visual-basic/language-reference/operators/addressof-operator) operator, which is also used in the CLR event handling model. |
| 34 | +- The [Handles](/dotnet/visual-basic/language-reference/statements/handles-clause) keyword in the event handler definition. For more information, see [Visual Basic and WPF event handling](/dotnet/desktop/wpf/advanced/visual-basic-and-wpf-event-handling?view=netframeworkdesktop-4.8&preserve-view=true). |
| 35 | +- The <xref:System.Windows.UIElement.AddHandler%2A?displayProperty=nameWithType> method, together with the `AddressOf` operator to reference the event handler. |
| 36 | + |
| 37 | +## Example |
| 38 | + |
| 39 | +The following example uses XAML to define a <xref:System.Windows.Controls.Button> named `ButtonCreatedByXaml` and to assign the `ButtonCreatedByXaml_Click` method as its <xref:System.Windows.Controls.Primitives.ButtonBase.Click> event handler. `Click` is a built-in routed event for buttons that derive from <xref:System.Windows.Controls.Primitives.ButtonBase>. |
| 40 | + |
| 41 | +:::code language="xaml" source="./snippets/how-to-add-an-event-handler-using-code/csharp/MainWindow.xaml" id="ButtonCreatedByXaml"::: |
| 42 | + |
| 43 | +The example uses code-behind to implement the `ButtonCreatedByXaml_Click` and `ButtonCreatedByCode_Click` handlers, and to assign the `ButtonCreatedByCode_Click` handler to the `ButtonCreatedByCode` and `StackPanel1` elements. Event handler methods can only be implemented in code-behind. |
| 44 | + |
| 45 | +:::code language="csharp" source="./snippets/how-to-add-an-event-handler-using-code/csharp/MainWindow.xaml.cs" id="ButtonEventHandlers"::: |
| 46 | +:::code language="vb" source="./snippets/how-to-add-an-event-handler-using-code/vb/MainWindow.xaml.vb" id="ButtonEventHandlers"::: |
| 47 | + |
| 48 | +When `ButtonCreatedByXaml` is clicked and its event handler runs, `ButtonCreatedByXaml_Click` programmatically: |
| 49 | + |
| 50 | +1. Adds a new button named `ButtonCreatedByCode` to the already constructed XAML element tree. |
| 51 | +1. Specifies properties for the new button, such as the name, content, and background color. |
| 52 | +1. Assigns the `ButtonCreatedByCode_Click` event handler to `ButtonCreatedByCode`. |
| 53 | +1. Assigns the same `ButtonCreatedByCode_Click` event handler to `StackPanel1`. |
| 54 | + |
| 55 | +When `ButtonCreatedByCode` is clicked: |
| 56 | + |
| 57 | +1. The <xref:System.Windows.Controls.Primitives.ButtonBase.Click> routed event is raised on `ButtonCreatedByCode`. |
| 58 | +1. The `ButtonCreatedByCode_Click` event handler assigned to `ButtonCreatedByCode` is triggered. |
| 59 | +1. The `Click` routed event traverses up the element tree to `StackPanel1`. |
| 60 | +1. The `ButtonCreatedByCode_Click` event handler assigned to `StackPanel1` is triggered. |
| 61 | +1. The `Click` routed event continues up the element tree potentially triggering other `Click` event handlers assigned to other traversed elements. |
| 62 | + |
| 63 | +The `ButtonCreatedByCode_Click` event handler obtains the following information about the event that triggered it: |
| 64 | + |
| 65 | +- The [sender](xref:System.Windows.RoutedEventHandler) object, which is the element that the event handler is assigned to. The `sender` will be `ButtonCreatedByCode` the first time the handler runs, and `StackPanel1` the second time. |
| 66 | +- The <xref:System.Windows.RoutedEventArgs.Source?displayProperty=nameWithType> object, which is the element that originally raised the event. In this example, the `Source` is always `ButtonCreatedByCode`. |
| 67 | + |
| 68 | +> [!NOTE] |
| 69 | +> A key difference between a routed event and a CLR event is that a routed event traverses the element tree, whereas a CLR event occurs only on the `sender`. As a result, a routed event `sender` can be any traversed element in the element tree. |
| 70 | +
|
| 71 | +For more information on how to create and handle routed events, see [How to create a custom routed event](/dotnet/desktop/wpf/advanced/how-to-create-a-custom-routed-event?view=netframeworkdesktop-4.8&preserve-view=true) and [Handle a routed event](/dotnet/desktop/wpf/advanced/how-to-handle-a-routed-event?view=netframeworkdesktop-4.8&preserve-view=true). |
| 72 | + |
| 73 | +## See also |
| 74 | + |
| 75 | +- [Routed events overview](/dotnet/desktop/wpf/advanced/routed-events-overview?view=netframeworkdesktop-4.8&preserve-view=true) |
| 76 | +- [XAML in WPF](/dotnet/desktop/wpf/advanced/xaml-in-wpf?view=netframeworkdesktop-4.8&preserve-view=true) |
0 commit comments