前言
通过反射获取到控件的路由事件时,如何获取到控件本身的事件,和通过Style设置的事件,这个问题似乎没有优美的解法。
stackoverflow中常见的方法通常是查询UIElement.EventHandlersStore获取控件对应的事件
https://stackoverflow.com/questions/44283395/copy-all-event-handlers-from-one-control-to-another-at-runtime
/// <summary>
/// Get a list of RoutedEventHandlers
/// Credit: Douglas : https://stackoverflow.com/a/12618521/3971575
/// </summary>
/// <param name="element"></param>
/// <param name="routedEvent"></param>
/// <returns></returns>
public 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.
PropertyInfo eventHandlersStoreProperty = typeof(UIElement).GetProperty("EventHandlersStore", BF.Instance | BF.NonPublic);
object eventHandlersStore = eventHandlersStoreProperty.GetValue(element, null);
// If no event handlers are subscribed, eventHandlersStore will be null.
// Credit: https://stackoverflow.com/a/16392387/1149773
if (eventHandlersStore == null)
return null;
// Invoke the GetRoutedEventHandlers method on the EventHandlersStore instance
// for getting an array of the subscribed event handlers.
MethodInfo getRoutedEventHandlers = eventHandlersStore.GetType().GetMethod("GetRoutedEventHandlers", BF.Instance | BF.Public | BF.NonPublic);