Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to watch WPF Routed Events?

I was wondering if there's a way to watch all RoutedEvents that are raised in a WPF application. A way to write some info about the events fired to the console would be prefect to see what's going on.

like image 762
Sorskoot Avatar asked Jul 14 '09 09:07

Sorskoot


People also ask

What are routed events in WPF?

A routed event is an event registered with the WPF event system, backed by an instance of the RoutedEvent class, and processed by the WPF event system. The RoutedEvent instance, obtained from registration, is typically stored as a public static readonly member of the class that registered it.

Why would you want to use routed commands instead of events?

Routed commands give you three main things on top of normal event handling: Routed command source elements (invokers) can be decoupled from command targets (handlers)—they do not need direct references to one another, as they would if they were linked by an event handler.

What is bubbling event in WPF?

A bubbling event begins with the element which triggers the event. Then it travels up the visual tree to the topmost element of the visual tree. So in WPF, the topmost element either could be a window or a usercontrol. Basically, an event bubbles up till it reached the topmost element.


1 Answers

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.

like image 88
Sorskoot Avatar answered Sep 28 '22 02:09

Sorskoot