什么是 WPF 预览事件? [英] What are WPF Preview Events?

查看:31
本文介绍了什么是 WPF 预览事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找事件Preview******"的描述,就像每个元素都有事件 KeyDown 和 PreviewKeyDown.有什么区别(不是一个附事件一个不是,真正的常规区别和编程方式区别)

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)

在从 Control 派生的任何类中,您都可以覆盖这两种方法.. OnKeyDown 和 OnPreviewKeyDown,现在我正在编写自定义控件,我应该使用哪种方法?以及两者的区别.

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.

推荐答案

来自 Programming WPF - Chris Sells 和 Ian Griffith

From Programming WPF - Chris Sells and Ian Griffith

除了直接事件,WPF 定义了大多数路由事件对 - 一个隧道和另一个冒泡.隧道事件名称总是以预览"开头,并且是首先提出.这让家长们有机会看到之前的事件到达孩子.接下来是冒泡的对应物.多数情况情况下,您将只处理冒泡之一.预览将是通常用于

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

  • 阻止事件 (e.Handled = true)
  • 让父级提前做一些事情来处理正常的事件.

例如如果 UI Tree = Button 包含 Grid 包含 Canvas 包含 Ellipse
单击椭圆会导致(MouseDownButton 被 Button 吃掉,而 Click 被抬起.)

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.)

PreviewMouseDownButton
PreviewMouseDownGrid
PreviewMouseDownCanvas
PreviewMouseDownEllipse
MouseDownEllipse
MouseDownCanvas
MouseDownGrid

这篇关于什么是 WPF 预览事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆