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

发布于 2024-12-05 14:55:02 字数 1821 浏览 1 评论 0原文

我有一个 WPF window,它作为模式对话框打开。

在对话框中,我有 OK &带有 IsDefault & 的 Cancel 按钮它们的 IsCancel 属性分别设置为 True。这两个按钮都有用于关闭对话框的 Click 事件处理程序。

这是相关的 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>

这是背后的代码:

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

当我按键盘上的 Esc 按钮时(即使焦点不在 Cancel 按钮上),对话框也会出现按预期关闭。但是,当我按 Enter 键且焦点不在 OK 按钮上时,什么也没有发生。

我在对话框上有一个DataGrid。当我选择数据网格中的任何行并按 Enter 键时,我想关闭该对话框。

如何实现这件事?

一些附加信息:对话框上有一个文本框。它具有 Keyboard.PreviewKeyDown 事件的事件处理程序。当我在文本框中按 Enter 键时,该对话框不应关闭。 但我可以删除这个处理程序。重要的是解决上述问题。

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

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

On dialog, I have OK & Cancel buttons with IsDefault & IsCancel properties set to True for them respectively. Both the buttons have Click event handlers which 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 Esc button on the keyboard (even when focus is not on the Cancel button), the dialog box gets closed as expected. However, when I press Enter key when focus is NOT on the OK button, nothing happens.

I have a DataGrid on the dialog. I want to close the dialog when I select any row in the data grid and press 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. When I am in the text box and I press enter, the dialog box should not be closed. 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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娜些时光,永不杰束 2024-12-12 14:55:02

你的代码对我来说工作得很好。当我按回车键时它会关闭对话框。你可以写 e.Handled = true; tbxSearchString_PreviewKeyDown 事件中搜索功能之后的行。所以它不会关闭对话框。

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

代码隐藏

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

Your code is working fine for me. it close dialog when I press enter. You can write e.Handled = true; line after your search functionality in tbxSearchString_PreviewKeyDown event. 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;
           }
        }
夜吻♂芭芘 2024-12-12 14:55:02

wpf 中没有内置的方法来关闭对话框窗口。您要做的就是为默认按钮设置 DialogResult。所以您所需要的只是以下内容:

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

代码隐藏:

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

there is no built-in way to close the dialog window in wpf. what you have to do, is to set the DialogResult for your default button. 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" />

codebehind:

    private void btnOK_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = true;
    }
谜兔 2024-12-12 14:55:02

您不应该自己调用 Close() 或处理 PreviewKeyDown

正确的方法是使用“确定”/“取消”按钮,并使用 Button.IsDefaultButton.IsCancelWindow.DialogResult。如果文本框中未处理“输入”按键,则按键将传播到窗口,并且将按下默认按钮。


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

现在在表单中的任何文本框中按 Enter 或 escape 将关闭表单(得到正确的结果)

You shouldn't be calling Close() or handling PreviewKeyDown yourself.

The proper way to do this is to have Ok/Cancel buttons, and use Button.IsDefault, Button.IsCancel, and Window.DialogResult. If the 'enter' press is unhandled in your textbox, the keypress will propagate to the 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 hitting enter or escape on any textbox in the form will close the form (with the proper result)

素手挽清风 2024-12-12 14:55:02

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

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

Just set the AcceptButton member to the button property name.

AcceptButton = btnOK;   // button used when ENTER is pressed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文