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

编程入门 行业动态 更新时间:2024-10-11 09:26:05
本文介绍了如何删除所有Click事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 如何可以删除Button的Click事件的所有事件处理程序?

我想从按钮中删除所有点击事件处理程序。我在Stack Overflow问题中发现这种方法如何从控件中删除所有事件处理程序 。

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

更多推荐

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

本文发布于:2023-11-02 10:09:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552092.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   程序   Click

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!