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

问题描述

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

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失去焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:38