什么是 WPF 预览事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1460170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are WPF Preview Events?
提问by Akash Kava
I have been looking for descriptions of events "Preview******" like every element has events KeyDown and PreviewKeyDown. What is difference (not that one is attached event and one is not, the real conventional difference and programming way difference)
我一直在寻找事件“Preview******”的描述,就像每个元素都有事件 KeyDown 和 PreviewKeyDown。有什么区别(不是一个是附加事件一个不是,真正的约定俗成的区别和编程方式的区别)
In any class derived from Control, you can override both methods.. OnKeyDown and OnPreviewKeyDown, now I am writing my custom control, which method shall I use? And whats difference between both of them.
在从 Control 派生的任何类中,您都可以覆盖这两种方法.. OnKeyDown 和 OnPreviewKeyDown,现在我正在编写自定义控件,我应该使用哪种方法?两者之间有什么区别。
回答by Gishu
From Programming WPF - Chris Sells and Ian Griffith
来自 WPF 编程 - Chris Sells 和 Ian Griffith
With the exception of direct events, WPF defines most routed events in pairs - one tunnelling and the other bubbling. The tunnelling event name always begins with 'Preview' and is raised first. This gives parents the chance to see the event before it reaches the child. This is followed by the bubbling counterpart. In most cases, you will handle only the bubbling one. The Preview would be usually used to
- block the event (
e.Handled = true
)- cause the parent to do something in advance to normal event handling.
除了直接事件,WPF 定义了大多数路由事件成对 - 一个隧道和另一个冒泡。隧道事件名称始终以“预览”开头,并首先引发。这让父母有机会在事件到达孩子之前看到它。其次是冒泡的对应物。在大多数情况下,您将只处理冒泡的问题。预览通常用于
- 阻止事件 (
e.Handled = true
)- 导致父母提前做一些事情来处理正常的事件。
e.g. if UI Tree = Button contains Grid contains Canvas contains Ellipse
Clicking on the ellipse would result in (MouseDownButton is eaten up by Button and Click is raised instead.)
例如,如果 UI Tree = Button contains Grid contains Canvas contains Ellipse
单击椭圆会导致(MouseDownButton 被 Button 吃掉,而 Click 被提升。)
PreviewMouseDownButton
PreviewMouseDownGrid
PreviewMouseDownCanvas
PreviewMouseDownEllipse
MouseDownEllipse
MouseDownCanvas
MouseDownGrid
回答by Bubblewrap
I found this blog entry really useful in describing the difference:
我发现这个博客条目在描述差异方面非常有用:
http://joshsmithonwpf.wordpress.com/2007/06/22/overview-of-routed-events-in-wpf/
http://joshsmithonwpf.wordpress.com/2007/06/22/overview-of-routed-events-in-wpf/
You have the visual tree, when an event occurs on an element in the tree, first a preview event will travel from the root to the element (tunneling): the PreviewKeyDown event will be raised on all these elements, and then a "normal" event will travel from the element to the root (bubbling).
你有可视化树,当事件发生在树中的元素上时,首先预览事件将从根传播到元素(隧道):PreviewKeyDown 事件将在所有这些元素上引发,然后是“正常”事件将从元素传播到根(冒泡)。
回答by Nick
Basically, it's the same event but happens right before the main event. They exist so you can listen for those types of events without interfering with the control's normal behavior when those events occur.
基本上,这是同一个事件,但发生在主要事件之前。它们的存在是为了让您可以侦听这些类型的事件,而不会在这些事件发生时干扰控件的正常行为。
For instance, buttons do things when you Click or MouseEnter, etc. If you handle those events yourself you have to make sure you do the same things otherwise your button won't act the same. The Preview events give you an event in the same timeline without having to worry about messing with existing functionality.
例如,按钮在您单击或鼠标输入等时执行操作。如果您自己处理这些事件,您必须确保您执行相同的操作,否则您的按钮将不会执行相同的操作。预览事件为您提供同一时间线中的事件,而不必担心弄乱现有功能。
This is especially useful when dealing with custom styles/triggers/control templates. When you start overriding control appearance/behavior.
这在处理自定义样式/触发器/控件模板时特别有用。当您开始覆盖控制外观/行为时。
So in your Control, do the main work you want in the OnKeyDown event and leave the preview event for someone else to use, is how I work with them.
因此,在您的 Control 中,在 OnKeyDown 事件中完成您想要的主要工作并将预览事件留给其他人使用,这就是我与他们合作的方式。
回答by jturinetti
This difference has to do with routed events, which is how WPF implements its event handling strategy. The standard event name (i.e. KeyDown, etc.) implies a bubbling routing strategy. The ones prepended with "Preview" (i.e. PreviewKeyDown, etc.) implies a tunneling routing strategy. You can read about these strategies in more detail here. Basically, when an event in WPF is invoked, it first travels from the top-most element down the visual tree to the element that invoked the event and finally returns upwards. On the way down the tree, you will encounter the PreviewKeyDown event, and on the return trip you will encounter the KeyDown event, in that order.
这种差异与路由事件有关,这就是 WPF 实现其事件处理策略的方式。标准事件名称(即 KeyDown 等)暗示了冒泡路由策略。前面带有“Preview”(即PreviewKeyDown 等)的那些意味着隧道路由策略。您可以在此处更详细地了解这些策略。基本上,当 WPF 中的事件被调用时,它首先从可视化树的最顶层元素向下移动到调用该事件的元素,最后向上返回。在沿着树向下的途中,您将遇到 PreviewKeyDown 事件,在回程中您将遇到 KeyDown 事件,按此顺序。