如何通过在wpf中按退格键来关注texbox [英] how to focus texbox by pressing backspace key in wpf

查看:80
本文介绍了如何通过在wpf中按退格键来关注texbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我....如何在按下退格键的同时关注文本框和其他控件....

Can anybody help me....how can i focus textbox and other controls while pressing backspace key....

推荐答案

在你的窗口上你应该添加PreviewKeyUp事件。

如果键是退格键,则将句柄设置为true。

调用RequestFocusChange()



On your window you should add the PreviewKeyUp event.
if the key is backspace, set handled to true.
Call RequestFocusChange()

PreviewKeyUp=Window_PreviewKeyUp







private void Window_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
   if (e.Key==System.Windows.Input.Key.Back)
    {
       e.Handled = true;
       RequestFocusChange(FocusNavigationDirection.Previous);
    }
}

private static bool RequestFocusChange(FocusNavigationDirection direction)
{
  var focused = Keyboard.FocusedElement as UIElement;
  if (focused != null) return focused.MoveFocus(new TraversalRequest(direction));
  return false;
}


这篇关于如何通过在wpf中按退格键来关注texbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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