Notes on Software

Routed Events in WPF

Posted by K. M. on August 9, 2008

Unlike the concept of dependency properties, the concept of routed events is a little difficult to understand. It is clear what purposes dependency properties serve. But routed events? Why would anyone want events to route up or down the element tree? There is a common example given of a Panel with multiple Buttons in it where one can attach an event handler for Button.Click on the Panel instead of on the Buttons and have it called when any Button is clicked. But that does not seem to be a good enough reason to architect an entire feature. And indeed it is not. Event routing becomes much more useful in the context of commands and control templating. It would be much more difficult to write controls like the ScrollBar or the Slider without routed commands (which work via routed events) while allowing them to be easily templatable. The real purpose of routed events is not to attach handlers on some node of a static tree of elements to handle events on the children. It is to enable parent elements to define behavior when a particular event occurs on a child element that it does not statically know of.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: