Equivalent to a keypreview property in WPF

后端 未结 3 788
春和景丽
春和景丽 2021-01-02 16:35

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

3条回答
  •  余生分开走
    2021-01-02 17:05

    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) {
    
      }
    

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

提交回复
热议问题