如何删除所有的Click事件处理程序?有的、事件、程序、Click

2023-09-03 01:05:13 作者:宇宙无敌小二逼

可能重复:   How请问可不可以删除按钮的Click事件的所有事件处理程序?

我想从一个按钮,删除所有的Click事件处理程序。我发现这个方法在堆栈溢出问题的 如何从控件中移除所有事件处理程序 的

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]);
}

不过,该行始终返回null:

But this line always returns null:

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

和这种方法是写于2006年。

And this method was written in 2006.

有没有这种方法的任何最新版本?

Is there any latest version of this method?

注:我与 WPF 和的 NET 的4.0

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

推荐答案

以下是用于检索所有已订阅的事件处理程序的任何路由事件一个有用的工具方法:

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;
}