How to use an enter key to send login information

I have a main 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 pressing the "Login" button if the user presses the "Enter" key from the password field?

Thanks!

+4
source share
2 answers

You can use the KeyDown PasswordBox KeyDown .

 <PasswordBox KeyDown="txtPassword_KeyDown"/> private void txtPassword_KeyDown(object sender, KeyRoutedEventArgs e) { if (e.Key == Windows.System.VirtualKey.Enter) //TODO: do login } 
+9
source

Use KeyDown event in PasswordBox -

 <PasswordBox KeyDown="PasswordKeyDown"/> 

Then, in your C # code, check if the enter key is pressed and log in as follows:

 using System.Windows.Input; private void PasswordKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) Login(); } 
+1
source

Source: https://habr.com/ru/post/1489521/


All Articles