0

我必须在页面中输入用户名和密码。在用户名文本框上单击输入时,我想将焦点移到密码框,焦点正确,但键盘关闭。如何保持键盘打开?

if (p.OriginalSource is PasswordBox)
{
        loginCommand.Execute(null);
}
else if (p.OriginalSource is TextBox) //assuming there is no third box that can handle
{
     var element = (p.OriginalSource as TextBox).FindName("passwordbox");
     var ss = ((WatermarkPasswordBox)element).Focus();
     ((WatermarkPasswordBox)element).UpdateLayout();
     //((WatermarkPasswordBox)element).Password = "";
}
4

2 回答 2

0

为您的用户名文本框制作 KeyDown 事件,并在这样的事件中编写代码

if (e.Key == System.Windows.Input.Key.Enter)
        {
            PasswordBox.Focus();
        }

这对我有帮助

于 2013-09-27T13:07:52.113 回答
0

好吧,即使经过太多努力,它对我的​​视图模型也不起作用。所以我从代码隐藏中尝试了这个并且它有效。

对于一种登录屏幕,我处理了用户名字段 ( ) 的 keydown 事件,并在 enter-keydown 事件上将TextBox焦点设置为密码字段 ( ) :PasswordBox

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key.Equals(Key.Enter))
    {
        PasswordBox.Focus();
    }
}
于 2014-02-23T19:09:15.593 回答