等效于WPF中的keypreview属性 [英] Equivalent to a keypreview property in WPF

查看:86
本文介绍了等效于WPF中的keypreview属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑为我的某些应用从WinForms跳入WPF,目前我正在研究条形码读取器/文本输入程序(医疗保健患者表格)的组合.

I'm pondering taking the plunge to WPF from WinForms for some of my apps, currently I'm working on the combined barcode-reader/text-entry program (healthcare patient forms).

为了能够处理条形码字符,我依靠WinForms中的Keypreview属性(因为可以扫描条形码,而不管哪个控件具有焦点).

To be able to process the barcode characters, I rely on the Keypreview property in WinForms (because barcodes can be scanned regardless of what control has the focus).

但是对于WPF应用程序,我似乎在VS2008或VS2010中都找不到KeyPreview属性.

But I cannot seem to find a KeyPreview property in neither VS2008 or VS2010, for a WPF app.

是否有其他方法/解决方案来处理WPF中的条形码字符?

Is there an alternative approach/solution to handle my barcode characters in WPF?

亨利牧师

推荐答案

在您自己的UserControls或Controls中使用替代(这是 UIElement 的替代)

use the override in your own UserControls or Controls (this is an override from UIElement)

protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e) {
     base.OnPreviewKeyDown(e);
  }

如果要向下预览未创建的任何元素上的键,可以执行以下操作:

if you want to preview the key down on any element which you dont create you can do this:

 Label label = new Label();
 label.PreviewKeyDown += new KeyEventHandler(label_PreviewKeyDown);

,然后有一个像这样的处理程序:-

and then have a handler like so :-

  void label_PreviewKeyDown(object sender, KeyEventArgs e) {

  }

如果将事件标记为已处理( e.Handled = true; ),这将停止引发KeyDown事件.

if you mark the event as handled (e.Handled = true;) this will stop the KeyDown event being raised.

这篇关于等效于WPF中的keypreview属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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