Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 326
Jader Dias Avatar asked Nov 16 '10 13:11

Jader Dias


2 Answers

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.

like image 110
Alex B Avatar answered Oct 04 '22 06:10

Alex B


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     } } 
like image 28
ihatemash Avatar answered Oct 04 '22 07:10

ihatemash