显示消息框后快捷键不起作用

发布于 2024-11-04 18:53:58 字数 1131 浏览 0 评论 0原文

我有一个在 InputBindings 中定义了键绑定的窗口。当我将焦点设置在表单上的任何控件上时,它们第一次起作用。

但是,当显示消息框并且我按“确定”时,除非我将焦点设置在窗口中的控件上,否则快捷键不起作用。

我的输入绑定:

<Window.InputBindings>
    <KeyBinding Gesture="Ctrl+N" Command="{x:Static local:MainWindow.NewMenuCommand}" />
    <KeyBinding Gesture="Ctrl+O" Command="{x:Static local:MainWindow.OpenMenuCommand}" />
    <KeyBinding Gesture="Ctrl+S" Command="{x:Static local:MainWindow.SaveMenuCommand}" />
    <KeyBinding Gesture="Ctrl+Q" Command="{x:Static local:MainWindow.CloseMenuCommand}" />
</Window.InputBindings>

我的命令绑定:

<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:MainWindow.NewMenuCommand}" Executed="NewEntity" />
    <CommandBinding Command="{x:Static local:MainWindow.OpenMenuCommand}" Executed="OpenEntity" />
    <CommandBinding Command="{x:Static local:MainWindow.SaveMenuCommand}" Executed="SaveEntity" />
    <CommandBinding Command="{x:Static local:MainWindow.CloseMenuCommand}" Executed="CloseEntity" />
</Window.CommandBindings>

I have a Window with Keybindings in InputBindings defined. They work the first time and when i set the focus on any control on the form.

But when a Messagbox is shown and I press "OK" they shortcut keys don't work until I set the focus on a control in my window.

My InputBindings:

<Window.InputBindings>
    <KeyBinding Gesture="Ctrl+N" Command="{x:Static local:MainWindow.NewMenuCommand}" />
    <KeyBinding Gesture="Ctrl+O" Command="{x:Static local:MainWindow.OpenMenuCommand}" />
    <KeyBinding Gesture="Ctrl+S" Command="{x:Static local:MainWindow.SaveMenuCommand}" />
    <KeyBinding Gesture="Ctrl+Q" Command="{x:Static local:MainWindow.CloseMenuCommand}" />
</Window.InputBindings>

My CommandBindings:

<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:MainWindow.NewMenuCommand}" Executed="NewEntity" />
    <CommandBinding Command="{x:Static local:MainWindow.OpenMenuCommand}" Executed="OpenEntity" />
    <CommandBinding Command="{x:Static local:MainWindow.SaveMenuCommand}" Executed="SaveEntity" />
    <CommandBinding Command="{x:Static local:MainWindow.CloseMenuCommand}" Executed="CloseEntity" />
</Window.CommandBindings>

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

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

发布评论

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

评论(1

你是我的挚爱i 2024-11-11 18:53:58

我不久前也遇到过这个问题,它与你的窗口的焦点有关,但我实际上找到了一个黑客来解决它,就像这样 -

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Activated="HandleWindowActivated"
    Title="Window1" Height="300" Width="300">

并且在你后面的代码中将焦点放在窗口上,像这样 -

private void HandleWindowActivated(object sender, EventArgs e)
{
    this.Focus();
} 

I also faced this issue while ago, it has something to do with the focus of your window but i actually found one hack to resolve it like this -

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Activated="HandleWindowActivated"
    Title="Window1" Height="300" Width="300">

And in your code behind put the focus on the window like this -

private void HandleWindowActivated(object sender, EventArgs e)
{
    this.Focus();
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文