Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The below is a helpful utility method for retrieving all subscribed event handlers for any routed event:</p> <pre><code>/// &lt;summary&gt; /// Gets the list of routed event handlers subscribed to the specified routed event. /// &lt;/summary&gt; /// &lt;param name="element"&gt;The UI element on which the event is defined.&lt;/param&gt; /// &lt;param name="routedEvent"&gt;The routed event for which to retrieve the event handlers.&lt;/param&gt; /// &lt;returns&gt;The list of subscribed routed event handlers.&lt;/returns&gt; 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; } </code></pre> <p>Using the above, the implementation of your method becomes quite simple:</p> <pre><code>private void RemoveClickEvent(Button b) { var routedEventHandlers = GetRoutedEventHandlers(b, ButtonBase.ClickEvent); foreach (var routedEventHandler in routedEventHandlers) b.Click -= (RoutedEventHandler)routedEventHandler.Handler; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload