2012-02-24 91 views
7

可能重复: How would that be possible to remove all event handlers of the Click event of a Button?如何删除所有Click事件处理程序?

我想从一个按钮,删除所有的单击事件处理程序。我在堆栈溢出问题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:

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

而且这种方法写于2006年

是否有此方法的任何最新版本?

注意:我正在使用WPF.NET 4.0。

+0

我可以对于为什么返回空值没有帮助,但@JonSkeet在http://stackoverflow.com/questions/6828054/how-would-that-be-possible-to-remove-all-event-handlers上有一个很好的答案。关于为什么不经过反思就无法实现这一点的事件。也许你需要以不同的方式解决问题。 – 2012-02-24 17:07:37

+1

你试图解决的根本问题是什么?你试图完成的目标是什么? – RQDQ 2012-02-24 17:53:21

+0

你意识到你正试图将WinForms代码应用于WPF? – Douglas 2012-02-24 19:07:27

回答

19

下面是用于检索所有订阅的事件处理程序的任何路由事件有帮助的实用方法:

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

使用上面,你的方法的实现变得十分简单:

private void RemoveClickEvent(Button b) 
{ 
    var routedEventHandlers = GetRoutedEventHandlers(b, ButtonBase.ClickEvent); 
    foreach (var routedEventHandler in routedEventHandlers) 
     b.Click -= (RoutedEventHandler)routedEventHandler.Handler; 
} 
+0

这是一个非常有用的答案,不幸附加到一个封闭的问题。我认为这个“重复”可以通过你在那里提供相同的答案来改善。 – phloopy 2012-08-21 01:36:23

+1

@phloopy:很好的建议。我刚刚发布了上述解决方案的[改进版本](http://stackoverflow.com/a/12618521/1149773)。 – Douglas 2012-09-27 09:48:05