Tuesday, November 15, 2011

Types of events in WPF or What is Event Routing or a Routed Event?


Event Routing or A Routed Event is a type of event that is has the ability to invoke handlers not only on on the object that raised the event but also on multiple listeners in an element tree. 
There are 2 types of events in WPF
1. Bubbling
2. Tunneling
3. Direct
Event Bubbling
It starts from Target and then bubbles up to the root  . Imagine that we have  Stack Panel inside Window , a rectangle control  inside the StackPanel.   If you we fire a bubbled event on a  rectangle ,  it will be first fired on the rectangle , then  StackPanel  and then on window .
  • Most routed events use the bubbling routing .
  • Bubbling routed events are  used to report input changes from  other UI elements.
  • All Main events Bubble.
ex: MouseDown
Event Tunneling
The order of events firing for the controls  in Event Tunneling is exactly opposite to the event Bubbling.  Do not get confused of the concept.  Let me make it very simple and clear. Imagine that we have  Stack Panel inside Window , a rectangle control  inside the StackPanel.   If you we fire  a tunneled event on a Window ,  it will be first fired on the Window, then  StackPanel  and then on rectangle .
  •   All the Preview events tunnels down
ex: PreviewMouseDown
Direct Routing This is similar to the "routing" that Windows Forms uses for events .These are also called  CLR events which we all are used to in .NET environment. Only that event fires.
ex: Mouse Enter
Advantages in RealTime
Routed events support a class handling mechanism in which the class  specifies static methods that  handles routed events before  registered instance handlers can access them. This is  concept is  useful in control design as custom class can enforce event-driven class behaviors 

No comments:

Post a Comment