Windows Phone:按下键盘输入时将焦点移动到文本框/

时间:2014-02-23 13:04:06

标签: xaml windows-phone-8

我有一个带有以下UI标记的Windows手机页面:

<StackPanel Orientation="Vertical">
    <TextBox Name="txtName"/>
    <PasswordBox Name="txtPassword"/>
    <Button Name="btnLogin" Content="Login"/>
<StackPanel>

我想做的事情:在用户输入用户名并单击键盘上的“Enter”键后,我希望焦点移动到下一个“txtPassword”TextBox,然后按“Enter” “焦点移动到按钮等等......

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

<强>的Xaml:

        <StackPanel Orientation="Vertical">
            <TextBox x:Name="txtName" KeyDown="TxtName_KeyDown" />
            <PasswordBox x:Name="txtPassword" KeyDown="TxtPassword_KeyDown"/>
            <Button x:Name="btnLogin" Content="Login" />
        </StackPanel>

<强> CS:

        private void TxtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                txtPassword.Focus();
        }

        private void TxtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnLogin.Focus();
        }

有用信息:Determining the Enter key is pressed in a TextBox