That??™s because the two events share the same instance of the RoutedEventArgs class.
Tunneling events are useful if you need to perform some preprocessing that acts on certain
keystrokes or filters out certain mouse actions. Figure 6-4 shows an example that tests
tunneling with the PreviewKeyDown event. When you press a key in the text box, the event is
fired first in the window and then down through the hierarchy. And if you mark the Preview-
KeyDown event as handled at any point, the bubbling KeyDown event won??™t occur.
CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 164
Figure 6-4. A tunneled key press
nTip Be careful about marking a tunneling event as handled. Depending on the way the control is written,
this may prevent the control from handling its own event (the related bubbling event) to perform some task
or update its state.
CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 165
IDENTIFYING THE ROUTING STRATEGY OF AN EVENT
Clearly, the different routing strategies affect how you??™ll use an event. But how do you determine what type of
routing a given event uses?
Tunneling events are straightforward. By .NET convention, a tunneling event always begins with the
word Preview (as in PreviewKeyDown). However, there??™s no similar mechanism to distinguish bubbling events
from direct events.
Pages:
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329