Is there a way to watch WPF Routed Events?

◇◆丶佛笑我妖孽 提交于 2019-12-02 20:58:50
Kent Boogaart

Yes, but it requires some reflection. You're better off using a tool like Snoop that already does the hard lifting for you.

In the tab Events you can see list of events, and the element that handled it.

I've found another way:

I've added this to the loaded handler of my UserControl.

var events = EventManager.GetRoutedEvents();
foreach (var routedEvent in events)
{
    EventManager.RegisterClassHandler(typeof(myUserControl), 
                                      routedEvent, 
                                      new RoutedEventHandler(handler));
}

and this is the handler method:

internal static void handler(object sender, RoutedEventArgs e)
{
    if (e.RoutedEvent.ToString() != "CommandManager.PreviewCanExecute" &&
            e.RoutedEvent.ToString() != "CommandManager.CanExecute")
        Console.WriteLine(e.OriginalSource+"=>"+e.RoutedEvent);
}

The CanExecute events are a bit too much in my case. If you would like to see these too, just remove the if statement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!