This documentation is archived and is not being maintained.

RoutedEventHandler Delegate

Represents the method that will handle various routed events that do not have specific event data beyond the data that is common for all routed events.

Namespace:  System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)

Public Delegate Sub RoutedEventHandler ( _
	sender As Object, _
	e As RoutedEventArgs _
Dim instance As New RoutedEventHandler(AddressOf HandlerMethod)
In XAML, you can use delegates but you cannot define your own.


Type: System.Object

The object where the event handler is attached.

Type: System.Windows.RoutedEventArgs

The event data.

The RoutedEventHandler delegate is used for any routed event that does not report event-specific information in the event data. There are many such routed events; prominent examples include Click and Loaded.

The most noteworthy difference between writing a handler for a routed event as opposed to a general common language runtime (CLR) event is that the sender of the event (the element where the handler is attached and invoked) cannot be considered to necessarily be the source of the event. The source is reported as a property in the event data (Source). A difference between sender and Source is the result of the event being routed to different elements, during the traversal of the routed event through an element tree.

You can use either sender or Source for an object reference if you are deliberately not interested in the routing behavior of a direct or bubbling routed event and you only intend to handle routed events on the elements where they are first raised. In this circumstance, sender and Source are the same object.

If you do intend to take advantage of the inherent features of routed events and write your handlers accordingly, the two most important properties of the event data that you will work with when writing event handlers are Source and Handled.

For certain combinations of input events and WPF control classes, the element that raises the event is not the first element that has the opportunity to handle it. If the input event has a Preview version of the event, then the root of the element tree has first opportunity, can set Handled to true in the shared event data, and can influence how the input event is reported to remaining elements in its event route. The Preview handling behavior can give the appearance that a particular routed event is not raised as expected. For more information, see Preview Events and Input Overview.

This example shows how to find the source element in an event handler.

The following example shows a Click event handler that is declared in a code-behind file. When a user clicks the button that the handler is attached to, the handler changes a property value. The handler code uses the Source property of the routed event data that is reported in the event arguments to change the Width property value on the Source element.

<Button Click="HandleClick">Button 1</Button>
Private Sub HandleClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'You must cast the object as a Button element, or at least as FrameworkElement, to set Width 
    Dim srcButton As Button
    srcButton = CType(e.Source, Button)
    srcButton.Width = 200
End Sub

For the complete sample, see Finding the Source Element in an Event Handler Sample.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0