[ACCEPTED]-What are WPF Preview Events?-events

Accepted answer
Score: 131

From Programming WPF - Chris Sells and Ian 15 Griffith

With the exception of direct events, WPF 14 defines most routed events in pairs - one 13 tunnelling and the other bubbling. The 12 tunnelling event name always begins with 11 'Preview' and is raised first. This gives 10 parents the chance to see the event before 9 it reaches the child. This is followed 8 by the bubbling counterpart. In most cases, you 7 will handle only the bubbling one. The 6 Preview would be usually used to

  • block the event (e.Handled = true)
  • cause the parent to do something in advance to normal event handling.

e.g. if 5 UI Tree = Button contains Grid contains 4 Canvas contains Ellipse
Clicking on the 3 ellipse would result in (MouseDownButton 2 is eaten up by Button and Click is raised 1 instead.)

PreviewMouseDownButton
PreviewMouseDownGrid
PreviewMouseDownCanvas
PreviewMouseDownEllipse
MouseDownEllipse
MouseDownCanvas
MouseDownGrid
Score: 10

I found this blog entry really useful in 8 describing the difference:

http://joshsmithonwpf.wordpress.com/2007/06/22/overview-of-routed-events-in-wpf/

You have the visual 7 tree, when an event occurs on an element 6 in the tree, first a preview event will 5 travel from the root to the element (tunneling): the 4 PreviewKeyDown event will be raised on all 3 these elements, and then a "normal" event 2 will travel from the element to the root 1 (bubbling).

Score: 4

This difference has to do with routed events, which 13 is how WPF implements its event handling 12 strategy. The standard event name (i.e. KeyDown, etc.) implies 11 a bubbling routing strategy. The ones prepended 10 with "Preview" (i.e. PreviewKeyDown, etc.) implies 9 a tunneling routing strategy. You can read 8 about these strategies in more detail here. Basically, when 7 an event in WPF is invoked, it first travels 6 from the top-most element down the visual 5 tree to the element that invoked the event 4 and finally returns upwards. On the way 3 down the tree, you will encounter the PreviewKeyDown 2 event, and on the return trip you will encounter 1 the KeyDown event, in that order.

Score: 3

Basically, it's the same event but happens 17 right before the main event. They exist 16 so you can listen for those types of events 15 without interfering with the control's normal 14 behavior when those events occur.

For instance, buttons 13 do things when you Click or MouseEnter, etc. If 12 you handle those events yourself you have 11 to make sure you do the same things otherwise 10 your button won't act the same. The Preview 9 events give you an event in the same timeline 8 without having to worry about messing with 7 existing functionality.

This is especially 6 useful when dealing with custom styles/triggers/control 5 templates. When you start overriding control 4 appearance/behavior.

So in your Control, do 3 the main work you want in the OnKeyDown 2 event and leave the preview event for someone 1 else to use, is how I work with them.

More Related questions