How to remove all Click event handlers?

14,315

The below is a helpful utility method for retrieving all subscribed event handlers for any routed event:

/// <summary>
/// Gets the list of routed event handlers subscribed to the specified routed event.
/// </summary>
/// <param name="element">The UI element on which the event is defined.</param>
/// <param name="routedEvent">The routed event for which to retrieve the event handlers.</param>
/// <returns>The list of subscribed routed event handlers.</returns>
public static RoutedEventHandlerInfo[] GetRoutedEventHandlers(UIElement element, RoutedEvent routedEvent)
{
    // Get the EventHandlersStore instance which holds event handlers for the specified element.
    // The EventHandlersStore class is declared as internal.
    var eventHandlersStoreProperty = typeof(UIElement).GetProperty(
        "EventHandlersStore", BindingFlags.Instance | BindingFlags.NonPublic);
    object eventHandlersStore = eventHandlersStoreProperty.GetValue(element, null);

    // Invoke the GetRoutedEventHandlers method on the EventHandlersStore instance 
    // for getting an array of the subscribed event handlers.
    var getRoutedEventHandlers = eventHandlersStore.GetType().GetMethod(
        "GetRoutedEventHandlers", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    var routedEventHandlers = (RoutedEventHandlerInfo[])getRoutedEventHandlers.Invoke(
        eventHandlersStore, new object[] { routedEvent });

    return routedEventHandlers;
}

Using the above, the implementation of your method becomes quite simple:

private void RemoveClickEvent(Button b)
{
    var routedEventHandlers = GetRoutedEventHandlers(b, ButtonBase.ClickEvent);
    foreach (var routedEventHandler in routedEventHandlers)
        b.Click -= (RoutedEventHandler)routedEventHandler.Handler;
}
Share:
14,315
Hasanuzzaman
Author by

Hasanuzzaman

A full stack software engineer with 7 years of experience in building large-scale applications. Pivotal contributions to the architecture and development life cycle of company projects. I am eager to use my technical skills and vast experiences in building .NET base application at your company.

Updated on June 05, 2022

Comments

  • Hasanuzzaman
    Hasanuzzaman almost 2 years

    Possible Duplicate: How would that be possible to remove all event handlers of the Click event of a Button?

    I want to remove all click event handlers from a button. I found this method in Stack Overflow question How to remove all event handlers from a control.

    private void RemoveClickEvent(Button b)
    {
        FieldInfo f1 = typeof(Control).GetField("EventClick",
                                                BindingFlags.Static |
                                                BindingFlags.NonPublic);
        object obj = f1.GetValue(b);
        PropertyInfo pi = b.GetType().GetProperty("Events",
                                                  BindingFlags.NonPublic |
                                                  BindingFlags.Instance);
        EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
        list.RemoveHandler(obj, list[obj]);
    }
    

    But this line always returns null:

      typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
    

    And this method was written in 2006.

    Is there any latest version of this method?

    Note: I am working with WPF and .NET 4.0.