What's the Best Way to Catch the Return Key in a PasswordBox? (WPF/XAML)

后端 未结 3 1903
-上瘾入骨i
-上瘾入骨i 2021-02-18 15:20

What\'s the best way to catch the return key in a PasswordBox? (WPF/XAML)

I have a TextBox field and a PasswordBox field on my login form (for username and password en

3条回答
  •  失恋的感觉
    2021-02-18 15:58

    You can handle the KeyDown event on the PasswordBox (and TextBox if desired) and then use the following event handler --

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Return && e.Key != Key.Enter)
            return;
        e.Handled = true;
        HandleEnter();
    }
    

提交回复
热议问题