0
votes

I have to fields in a page, username & password. while clicking enter on username TextBox i want to move focus to PasswordBox, the focus comes correctly but the keyboard gets closed. how can i keep the keyboard open?

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 = "";
}
2
Did you solve this issue ? - Mina Wissa
@MinaSamy yes i solved it somehow. it didn't work me from viewmodel, but worked when i tried through code behind. - Ankit
No problem, would you please share the solution ? - Mina Wissa
@MinaSamy plz see the answer i have just added. - Ankit

2 Answers

0
votes

Make KeyDown event for your username textbox and write code inside the event like this

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

This helped for me

0
votes

well it didn't work for me from view-model even after too much efforts. so i tried this from code-behind and it worked.

for a kind of login screen, i handled the keydown event of the username field (TextBox) and set the focus to password filed (PasswordBox) on enter-keydown event:

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key.Equals(Key.Enter))
    {
        PasswordBox.Focus();
    }
}