Files
docs-desktop/dotnet-desktop-guide/net/wpf/events/how-to-add-an-event-handler-using-code.md
T
Tris ShoresandAndy De George 8e9afae7f6 Content update - How to add an event handler using code (user story 1878475) (#1287)
* Add article, redirects, toc

* Add alternative event handler assignments (commented)

* Update dotnet-desktop-guide/net/wpf/events/how-to-add-an-event-handler-using-code.md

Co-authored-by: Andy (Steve) De George <[email protected]>

* Update dotnet-desktop-guide/net/wpf/events/how-to-add-an-event-handler-using-code.md

Co-authored-by: Andy (Steve) De George <[email protected]>

* Further edits

Co-authored-by: Andy (Steve) De George <[email protected]>
2022-02-01 14:23:23 -08:00

5.5 KiB

title, description, ms.date, dev_langs, helpviewer_keywords
title description ms.date dev_langs helpviewer_keywords
How to add an event handler using code Learn how to add an event handler in code-behind for an element in Windows Presentation Foundation (WPF). 02/01/2021
csharp
vb
event handlers [WPF], adding
XAML [WPF], adding event handlers

How to add an event handler using code (WPF .NET)

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:

  • You assign an event handler to an element after the markup page that contains the element loads.
  • You add an element and assign its event handler after the markup page that will contain the element loads.
  • You define the element tree for your application entirely in code.

[!INCLUDE desktop guide under construction]

Syntax for event handler assignment

C# supports event handler assignment using:

VB supports event handler assignment using:

Example

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.

:::code language="xaml" source="./snippets/how-to-add-an-event-handler-using-code/csharp/MainWindow.xaml" id="ButtonCreatedByXaml":::

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.

:::code language="csharp" source="./snippets/how-to-add-an-event-handler-using-code/csharp/MainWindow.xaml.cs" id="ButtonEventHandlers"::: :::code language="vb" source="./snippets/how-to-add-an-event-handler-using-code/vb/MainWindow.xaml.vb" id="ButtonEventHandlers":::

When ButtonCreatedByXaml is clicked and its event handler runs, ButtonCreatedByXaml_Click programmatically:

  1. Adds a new button named ButtonCreatedByCode to the already constructed XAML element tree.
  2. Specifies properties for the new button, such as the name, content, and background color.
  3. Assigns the ButtonCreatedByCode_Click event handler to ButtonCreatedByCode.
  4. Assigns the same ButtonCreatedByCode_Click event handler to StackPanel1.

When ButtonCreatedByCode is clicked:

  1. The xref:System.Windows.Controls.Primitives.ButtonBase.Click routed event is raised on ButtonCreatedByCode.
  2. The ButtonCreatedByCode_Click event handler assigned to ButtonCreatedByCode is triggered.
  3. The Click routed event traverses up the element tree to StackPanel1.
  4. The ButtonCreatedByCode_Click event handler assigned to StackPanel1 is triggered.
  5. The Click routed event continues up the element tree potentially triggering other Click event handlers assigned to other traversed elements.

The ButtonCreatedByCode_Click event handler obtains the following information about the event that triggered it:

Note

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.

For more information on how to create and handle routed events, see How to create a custom routed event and Handle a routed event.

See also