How to make a submit button in WPF?
Asked Answered
M

5

73

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?

Mackenie answered 16/11, 2010 at 13:13 Comment(1)
Is there a key preview property in WPF ? If so, setting it to true will send all key press directly to the form and you could use the keypress event to do it.Obduliaobdurate
P
154

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.

Proficient answered 16/11, 2010 at 13:53 Comment(2)
That was my first suggestion in my answer below but then i noticed XMLforDummies didn't say he/she had an "OK" or "Submit" button on the form so i edited my answer. The only "problem" you may run into with this solution is, if a button other than the "OK"/"Submit" button has focus, pressing the enter key will only click the focused button and will not trigger the other event which is probably what you expect but just wanted to bring that point up.Frostbitten
I think that behavior is intentional. For example, if that button opened a modal dialog, and by hitting the enter key it trigger the submit, you wouldn't want both events to fire.Proficient
F
9

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
    }
}
Frostbitten answered 16/11, 2010 at 13:17 Comment(4)
Sorry, I just realized you didn't say you have a button on the form. You must have a button on the form before this will work.Frostbitten
If not, just add IsHitTestVisible="true" to the Window propertiesWoken
Warning: This event will trigger on any control in the Window, even other buttons. Which is not normally an issue unless your users Tab around the form and use Enter to click buttons.Doug
@WalterStabosz when you tab around, you should use anyway the space button to activate the highlighted button :)Lanellelanette
I
1

According to this https://wpf.2000things.com/tag/isdefault/

IsDefaulted will be true if all of the following is true:

  1. IsDefault for the Button is set to true
  2. Some other control currently has focus
  3. The control that currently has focus does not itself handle the ENTER key

In my case I was trying to hit Enter from TextBox which already handles Enter Key. Turnaround was to set AcceptsReturn = false for TextBox. This option works if you do not need multiline textbox and Enter Key should submit entered text. Also Text Binding should be updated while typing

<TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" 
 AcceptsReturn="False"/>
Intend answered 4/10, 2022 at 0:38 Comment(0)
D
0

I found that the DatePicker control will swallow the Enter keypress so the default button doesn't get clicked. I wrote this event handler to fix that. Use the PreviewKeyUp to ensure that the DatePicker performs its date formatting code before clicking the default button.

private void DatePicker_PreviewKeyUp(object sender, KeyEventArgs e) {
    // event handler to click the default button when you hit enter on DatePicker
    if (e.Key == Key.Enter) {
        // https://www.codeproject.com/Tips/739358/WPF-Programmatically-click-the-default-button
        System.Windows.Input.AccessKeyManager.ProcessKey(null, "\x000D", false);
    }
}
Doug answered 16/1, 2018 at 16:40 Comment(0)
P
0
<Button Name="btnDefault" IsDefault="true" Click="OnClickDefault">OK</Button>

You can do this by setting the button IsDefault property to True.

You can get detailed information from Microsoft's documents.

Button.IsDefault Property

Petterson answered 22/3, 2021 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.