gpt4 book ai didi

wpf - 什么是 WPF 预览事件?

转载 作者:行者123 更新时间:2023-12-03 05:18:25 27 4
gpt4 key购买 nike

我一直在寻找事件“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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com