This documentation is archived and is not being maintained.

ContentElement.RemoveHandler Method

Removes the specified routed event handler from this element.

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

public void RemoveHandler (
	RoutedEvent routedEvent,
	Delegate handler
public final void RemoveHandler (
	RoutedEvent routedEvent, 
	Delegate handler
public final function RemoveHandler (
	routedEvent : RoutedEvent, 
	handler : Delegate
You cannot use methods in XAML.



The identifier of the routed event for which the handler is attached.


The specific handler implementation to remove from the event handler collection on this element.

The most common scenario for using this API is when you implement the common language runtime (CLR) "wrapper" event that is associated with a custom routed event, specifically when you implement the "remove" logic for handlers at the CLR level. The example that follows this remarks section illustrates this scenario.

Calling this method has no effect if there were no handlers registered with criteria that matches the input parameters for the method call.

If more than one handler is attached that matched the criteria, only the first handler in the event handler store is removed. This behavior is consistent with CLR behavior of the -= operator.

Neither routedEvent nor handler may be a null reference (Nothing in Visual Basic). Attempting to provide either value as a null reference (Nothing in Visual Basic) will raise an exception.

This method ignores the handledEventsToo parameter information, which is provided if the handler was first added with the AddHandler(RoutedEvent,Delegate,Boolean) signature that enables handling of already-handled events. Either type of handler is removed.

This example shows how bubbling events work and how to write a handler that can process the routed event data.

In Windows Presentation Foundation (WPF), elements are arranged in an element tree structure. The parent element can participate in the handling of events that are initially raised by child elements in the element tree. This is possible because of event routing.

Routed events typically follow one of two routing strategies, bubbling or tunneling. This example focuses on the bubbling event and uses the ButtonBase.Click event to show how routing works.

The following example uses XAML attribute syntax to attach an event handler to a common parent element, which in this example is StackPanel. Instead of attaching individual event handlers for each Button child element, the example uses attribute syntax to attach the event handler to the StackPanel parent element. This event-handling pattern shows how to use event routing as a technique for reducing the number of elements where a handler is attached. All the bubbling events for each Button route through the parent element.

Note that on the parent StackPanel element, the Click event name specified as the attribute is partially qualified by naming the Button class. The Button class is a ButtonBase derived class that has the Click event in its members listing. This partial qualification technique for attaching an event handler is necessary if the event that is being handled does not exist in the members listing of the element where the routed event handler is attached.

      <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
  <Button Name="Button1">Item 1</Button>
  <Button Name="Button2">Item 2</Button>
  <TextBlock Name="results"/>

For the complete sample, see RoutedEvent Handling Sample.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0