我在InputBindings中定义了一个带有键绑定的窗口。他们第一次工作,当我把重点放在表格上的任何控件上时。
但是当显示Messagbox并按“OK”时,他们的快捷键不起作用,直到我将焦点设置在窗口中的控件上。
我的输入绑定:
<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>
我的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>
答案 0 :(得分:1)
我前面也遇到过这个问题,它与你的窗口的焦点有关但我实际上发现了一个黑客来解决它 -
<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();
}