How to close a WPF window (dialog box) when pressing Enter?

I have a WPF window that opens as a modal dialog.

In the dialog box, I have the OK and Cancel with the IsDefault and IsCancel set to True for them respectively. Both buttons have Click event handlers that close the dialog box.

Here is the relevant XAML:

 <StackPanel Orientation="Horizontal" Grid.Row="1" Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="190"> <Button Content="OK" Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" /> <Button Content="Cancel" Height="25" Margin="10,10,10,10" Width="75" Name="btnCancel" TabIndex="1700" IsCancel="True" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="btnCancel_Click" /> </StackPanel> 

Here is the code behind:

 private void btnOK_Click(object sender, RoutedEventArgs e) { // My some business logic is here this.Close(); } private void btnCancel_Click(object sender, RoutedEventArgs e) { this.Close(); } 

When I press the Esc button on the keyboard (even if the focus is not on the Cancel button), the dialog box closes as expected. However, when I press the Enter key when the focus is not on the OK button, nothing happens.

I have a DataGrid in a dialog box. I want to close the dialog when I select any row in the data grid and press enter.

How to do it?

Additional information: there is a text field in the dialog box. And it has an event handler for Keyboard.PreviewKeyDown . When I'm in the text box and I press the enter button, the dialog should not close. But I can remove this handler. It is important to resolve the issue.

 private void tbxSearchString_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { this.Search(); // Does some searching } } 
+6
source share
4 answers

Your code works fine for me. this closes the dialog when I press enter. You can write e.Handled = true; after the search function in the tbxSearchString_PreviewKeyDown event. Therefore, the dialogue will not be closed.

 <Grid> <TextBox Name="tbxSearchString" HorizontalAlignment="Left" Width="100" Height="30" Grid.Row="0" PreviewKeyDown="tt_PreviewKeyDown"></TextBox> <StackPanel Orientation="Horizontal" Grid.Row="1" Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="190"> <Button Content="OK" Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" /> <Button Content="Cancel" Height="25" Margin="10,10,10,10" Width="75" Name="btnCancel" TabIndex="1700" IsCancel="True" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="btnCancel_Click" /> </StackPanel> </Grid> 

Code for

 private void btnOK_Click(object sender, RoutedEventArgs e) { DialogResult = true; } private void btnCancel_Click(object sender, RoutedEventArgs e) { this.Close(); } private void tbxSearchString_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { this.Search(); e.Handled = true; } } 
+7
source

There is no built-in way to close the dialog in wpf. you need to set DialogResult for your button by default. so all you need is the following:

Xaml

 <Button Content="OK" Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" /> 

separated code:

  private void btnOK_Click(object sender, RoutedEventArgs e) { DialogResult = true; } 
+8
source

You should not call Close() or process PreviewKeyDown yourself.

The correct way to do this is to use the Ok / Cancel buttons and use Button.IsDefault , Button.IsCancel and Window.DialogResult . If the 'enter' press is not processed in your text box, the key will extend to Window , and the default button will be pressed.


MyForm.xaml:

 <Button x:Name="btnOk" Content="Ok" Click="btnOk_Click" IsDefault="True"/> <Button x:Name="btnCancel" Content="Cancel" Click="btnCancel_Click" IsCancel="True"/> 

MyForm.xaml.cs:

 private void btnOk_Click(object sender, RoutedEventArgs e) { DialogResult = true; } private void btnCancel_Click(object sender, RoutedEventArgs e) { DialogResult = false; } 

Now pressing input or output in any text field on the form will close the form (with the correct result)

+5
source

Just set the AcceptButton member to the button property name.

 AcceptButton = btnOK; // button used when ENTER is pressed 
0
source

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


All Articles