How to make a submit button in WPF?
When you press Enter anywhere in a HTML form
it triggers its action
, that is equivalent of pressing the submit
button.
How to make a window that when I press Enter anywhere it will trigger an event?
Solution 1:
Set the IsDefault
property on the button to true to enable the Enter key to activate that button's action. There is also the IsCancel
property that does the same thing for the Escape key.
Solution 2:
Assign the PreviewKeyDown
event to the window in XAML then check the KeyEventArgs
in codebehind to determine if the user pressed the Enter key.
XAML code:
<Window
[...]
PreviewKeyDown="Window_PreviewKeyDown">
Code behind:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// Whatever code you want if enter key is pressed goes here
}
}