wpf - 什么是 WPF 预览事件?

标签 wpf events

我一直在寻找事件“Preview******”的描述,就像每个元素都有事件 KeyDown 和 PreviewKeyDown 一样。有什么区别(不是一个是附加事件一个不是,真正的约定的区别和编程方式的区别)

在任何从 Control 派生的类中,您都可以重写这两个方法。OnKeyDown 和 OnPreviewKeyDown,现在我正在编写自定义控件,我应该使用哪个方法?他们两者有什么区别。

最佳答案

来自 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.

例如如果 UI Tree = 按钮包含网格包含 Canvas 包含椭圆
单击椭圆会导致(MouseDownButton 被 Button 吃掉,而 Click 被提升。)

PreviewMouseDownButton
PreviewMouseDownGrid
PreviewMouseDownCanvas
PreviewMouseDownEllipse
MouseDownEllipse
MouseDownCanvas
MouseDownGrid

关于wpf - 什么是 WPF 预览事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1460170/

相关文章:

android - Cordova 3.4 - 检测键盘事件

wpf - 将WPF样式应用于子项

.net - 如何在 WPF 应用程序中生成 "print preview"的 FlowDocument?

c# - 数据绑定(bind)/PropertyChanged 为空

Jquery 仅检测点击事件

cocoa - 如何防止递归 KVO 通知?

c# - 无法在 TextChanged 中使用撤消

wpf - 逻辑组合依赖属性

java - 从 jcombobox 中移除焦点后,itemlistener 获取事件

javascript - 如何维护套接字事件内连接的每个用户的值(value)