Question

In a TextBox input. After type enter key, i want to hide soft keyboard. How to do it in codes?

private void OnKeyDownHandler(object sender, KeyEventArgs e)
           {
                if (e.Key != Key.Enter)
                   return;         

...}
Était-ce utile?

La solution

this.focus() This will allow the focus to be lost from the textbox. It basically puts the focus on the page instead. You could also convert your textbox to read only to disallow any further input.

Hiding the SIP can be done by simply changing the focus from the textbox to any other element on the page. It does not have to be this.focus(), it could be anyElement.focus(). As long as the element is not your textbox, the SIP should hide itself.

Autres conseils

I use the following method to dismiss the SIP:

/// 
/// Dismisses the SIP by focusing on an ancestor of the current element that isn't a
/// TextBox or PasswordBox.
/// 
public static void DismissSip()
{
    var focused = FocusManager.GetFocusedElement() as DependencyObject;

    if ((null != focused) && ((focused is TextBox) || (focused is PasswordBox)))
    {
        // Find the next focusable element that isn't a TextBox or PasswordBox
        // and focus it to dismiss the SIP.
        var focusable = (Control)(from d in focused.Ancestors()
                                  where
                                    !(d is TextBox) &&
                                    !(d is PasswordBox) &&
                                    d is Control
                                  select d).FirstOrDefault();
        if (null != focusable)
        {
            focusable.Focus();
        }
    }
 }

The Ancestors method comes from LinqToVisualTree by Colin Eberhardt. The code is used in conjunction with an Enter key handler, for "tabbing" to the next TextBox or PasswordBox, which is why they're skipped in the selection, but you could include them if it makes sense for you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top