简体   繁体   English

按Enter键时如何关闭WPF窗口(对话框)?

[英]How to close WPF window (dialog box) when Enter key is pressed?

I have a WPF window which opens as a modal dialog. 我有一个WPF window ,它作为模态对话框打开。

On dialog, I have OK & Cancel buttons with IsDefault & IsCancel properties set to True for them respectively. 在对话框中,我有OKCancel按钮, IsDefaultIsCancel属性分别设置为True Both the buttons have Click event handlers which close the dialog box. 这两个按钮都有Click事件处理程序,可以关闭对话框。

Here is the relevant XAML: 这是相关的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 Esc button on the keyboard (even when focus is not on the Cancel button), the dialog box gets closed as expected. 当我按下键盘上的Esc按钮时(即使焦点不在“ Cancel按钮上),对话框也会按预期关闭。 However, when I press Enter key when focus is NOT on the OK button, nothing happens. 但是,当我按下OK按钮时焦点不按Enter键时,没有任何反应。

I have a DataGrid on the dialog. 我在对话框上有一个DataGrid I want to close the dialog when I select any row in the data grid and press enter. 我想在数据网格中选择任何行并按Enter键时关闭对话框。

How to make this thing happen? 怎么让这件事发生?

Some additional information: I have a text box on the dialog. 一些附加信息:对话框中有一个文本框。 And it has the event handler for the Keyboard.PreviewKeyDown event. 它有Keyboard.PreviewKeyDown事件的事件处理程序。 When I am in the text box and I press enter, the dialog box should not be closed. 当我在文本框中并按Enter键时,不应关闭对话框。 But I can remove this handler. 但是我可以删除这个处理程序。 Important thing is to get above question resolved. 重要的是要解决上面的问题。

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

Your code is working fine for me. 你的代码对我来说很好。 it close dialog when I press enter. 按Enter键时关闭对话框。 You can write e.Handled = true; 你可以写e.Handled = true; line after your search functionality in tbxSearchString_PreviewKeyDown event. 在tbxSearchString_PreviewKeyDown事件中搜索功能之后的行。 So it will not close dialog. 所以它不会关闭对话框。

<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 behind 代码背后

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;
           }
        }

there is no built-in way to close the dialog window in wpf. 没有内置的方法来关闭wpf中的对话框窗口。 what you have to do, is to set the DialogResult for your default button. 你要做的是将DialogResult设置为默认按钮。 so all you need is the following: 所以你需要的是以下内容:

xaml 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" />

codebehind: 代码隐藏:

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

You shouldn't be calling Close() or handling PreviewKeyDown yourself. 您不应该自己调用Close()或处理PreviewKeyDown

The proper way to do this is to have Ok/Cancel buttons, and use Button.IsDefault , Button.IsCancel , and Window.DialogResult . 执行此操作的正确方法是使用“确定/取消”按钮,并使用Button.IsDefaultButton.IsCancelWindow.DialogResult If the 'enter' press is unhandled in your textbox, the keypress will propagate to the Window and the default button will be pressed. 如果在文本框中未处理“输入”按钮,则按键将传播到Window并且将按下默认按钮。


MyForm.xaml: 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: MyForm.xaml.cs:

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

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

Now hitting enter or escape on any textbox in the form will close the form (with the proper result) 现在点击表单中任何文本框的输入或转义将关闭表单(具有正确的结果)

只需将AcceptButton成员设置为按钮属性名称即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM