如何使TextBox失去焦点? [英] How to make TextBox lose its focus?

查看:84
本文介绍了如何使TextBox失去焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户触摸 Enter 虚拟键时,如何使 TextBox 失去焦点并隐藏屏幕键盘?

How to make TextBox lose its focus and hide onscreen keyboard when user touches Enter virtual key?

    private void TheName_KeyDown(object sender, KeyRoutedEventArgs e) {
        var tb = sender as TextBox;
        if (e.Key == Windows.System.VirtualKey.Enter) {
            // ... tb.LooseTheFocus_PLEASE(); !???
        }
    }

推荐答案

    /// <summary>
    /// Makes virtual keyboard disappear
    /// </summary>
    /// <param name="sender"></param>
    private void LoseFocus(object sender) {
        var control = sender as Control;
        var isTabStop = control.IsTabStop;
        control.IsTabStop = false;
        control.IsEnabled = false;
        control.IsEnabled = true;
        control.IsTabStop = isTabStop;
    }

    /// <summary>
    /// Makes virtual keyboard disappear when user taps enter key
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void LooseFocusOnEnter(object sender, KeyRoutedEventArgs e) {
        if (e.Key == Windows.System.VirtualKey.Enter) {
            e.Handled = true; LoseFocus(sender);
        }
    }

这很丑.但这行得通.关键部分是IsTabStop属性.如果我不触摸它,键盘会消失一秒钟,然后再次出现.

It's ugly. But it works. The key part is IsTabStop property. If I don't touch it - the keyboard disappers for a fraction of a second and reapears again.

这篇关于如何使TextBox失去焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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