Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the enter key to submit login details

I have a basic login page,

    <TextBox x:Name="UsernameInput" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Username, Mode=TwoWay}" VerticalAlignment="Center" Grid.Row="0" Grid.Column="2" Width="400" />
    <PasswordBox HorizontalAlignment="Left" VerticalAlignment="Center" Password="{Binding Password, Mode=TwoWay}" Grid.Row="1" Grid.Column="2" Width="400"/>
    <TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="Username: " VerticalAlignment="Center" Margin="0,0,0,15" Grid.Row="0" Grid.Column="0" Style="{StaticResource SubheaderTextStyle}"/>
    <TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="Password: " VerticalAlignment="Center" Margin="0,0,0,15" Grid.Row="1" Grid.Column="0" Style="{StaticResource SubheaderTextStyle}"/>

    <Button Content="Login" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="2" Grid.Column="2" Height="50" Width="300" Command="{Binding LoginCommand}"/>

How can I simulate the "Login" button being pressed if the user hits the Enter key from the password field?

Thanks!

like image 364
A O Avatar asked Jul 03 '13 15:07

A O


1 Answers

You can use KeyDown event of PasswordBox.

<PasswordBox KeyDown="txtPassword_KeyDown"/>

private void txtPassword_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Enter)
        //TODO: do login
}
like image 135
Farhan Ghumra Avatar answered Oct 03 '22 21:10

Farhan Ghumra