Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are routed events and how it's different from normal events

Tags:

wpf

I will appreciate if some body can explain with a simple example.

like image 886
DJay Avatar asked Aug 04 '10 11:08

DJay


People also ask

What are routed events?

From a functional perspective, a routed event is a type of event that can invoke handlers on multiple listeners in an element tree, not just on the event source. An event listener is the element where an event handler is attached and invoked. An event source is the element or object that originally raised an event.

Why should we 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 the exact difference between bubbling events and tunneling events?

The difference between the two, as the naming convention implies, is that a tunneling event will start at the highest node in the tree (probably the Window) and going down to the lowest child. A bubbling event will start at the child and then go upwards again.

What is RoutedEventArgs?

Remarks. RoutedEventArgs is a common event data type used for base element events in UWP app using C++, C#, or Visual Basic. Generally RoutedEventArgs as the event data type indicates that the event with this event data is a routed event, although there are some exceptions.


1 Answers

Imagine a Window containing a dense hierarchy of child controls. Now let's say you want to do something, there's a right click anywhere in your window.

  • With normal events, you'd have to handle a Click event for all controls, because you're not sure where the user might click.
  • With WPF's routed events, the events either "bubble" or "tunnel" (i.e travel up the UI tree or down) if they dont find an event handler, which "handles" it at the current level. So you could write one handler for the window's event i.e. TopLevel. (WPF has a convention of event pairs, PreviewXXX and XXX - the PreviewXXX event fires first and tunnels down from root to control which received the stimulus and the counterpart XXX event then bubbles up from child control back upto Root). So if you right click a button, WPF travels up the UI hierarchy, invoking all handlers that it finds (unless someone marks the event has "handled" in the event args.)
like image 71
Gishu Avatar answered Sep 19 '22 20:09

Gishu