What are WPF Preview Events?

I was looking for event descriptions of "Preview ******", how each element has KeyDown and PreviewKeyDown events. What is the difference (not that one connected event, but not the other, real conditional difference and programming difference)

In any class derived from Control, you can override both methods. OnKeyDown and OnPreviewKeyDown, now I am writing my own control, which method should I use? And what is the difference between them.

+62
events wpf
Sep 22 '09 at 13:40
source share
4 answers

From WPF Programming - Chris Sells and Ian Griffith

With the exception of direct events, WPF defines most routable events in pairs β€” one tunneling and the other bubbling. The name of the tunnel event always starts with "Preview" and is raised first. This gives parents a chance to see the event before it reaches the child. This is followed by a bubble copy. In most cases, you will only handle the bubble one. Preview is commonly used for

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

eg. if UI Tree = Button contains Grid contains Canvas contains Ellipse
Clicking on the ellipse will cause (MouseDownButton to be eaten by Button and Click raised instead.)

 PreviewMouseDownButton PreviewMouseDownGrid PreviewMouseDownCanvas PreviewMouseDownEllipse MouseDownEllipse MouseDownCanvas MouseDownGrid 
+104
Sep 22 '09 at 13:57
source share

I found this blog post very useful for describing the difference:

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

You have a visual tree, when an event occurs in an element in the tree, first the preview event moves from the root to the element (tunneling): the PreviewKeyDown event will be raised on all these elements, and then the β€œnormal” event will move from the element to the root (bubbles )

+7
Sep 22 '09 at 13:52
source share

In principle, this is one and the same event, but occurs right before the main event. They exist, so you can listen to those types of events without interfering with the normal behavior of the control when these events occur.

For example, buttons do something when pressed or MouseEnter, etc. If you handle these events yourself, you must make sure that you do the same, otherwise your button will not act the same. Preview events provide you with an event on the same timeline without worrying about messing with existing functionality.

This is especially useful when working with custom styles / triggers / control patterns. When you start to cancel the appearance / behavior of the control.

So, in your control, do the basic work you want in the OnKeyDown event, and leave a preview event for someone else how I work with them.

+3
Sep 22 '09 at 13:43
source share

This difference is related to routed events, since WPF implements its event handling strategy. The standard event name (i.e., KeyDown, etc.) implies a routing strategy. Those preceded by "Preview" (ie PreviewKeyDown, etc.) imply a tunneling routing strategy. You can read about these strategies in more detail here . Basically, when an event in WPF is called, it first moves from the topmost element down the visual tree to the element that triggers the event, and finally returns up. On the way down the tree, you will encounter the PreviewKeyDown event, and on the way back, you will encounter the KeyDown event in that order.

+3
Sep 22 '09 at 14:01
source share



All Articles