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) {
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();
source share