2011-04-29 77 views
1

我有一个定义了InputBindings中的键绑定的窗口。他们第一次工作,当我把重点放在窗体上的任何控件上。显示消息框后没有工作的快捷键

但是,当显示消息框并按下“确定”时,他们的快捷键不起作用,直到我将焦点设置在窗口中的控件上。

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

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

回答

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

它必须处理没有定义消息框的父/所有者。通过使用 MessageBox.Show(我,“保存”) 在代替: MSGBOX(“保存”) 制造它解决这个问题 – 2011-04-29 15:06:17

+0

好吧,其实在我的情况我已经实现了我的自定义消息框设置父母/孩子关系也不适合我。这就是为什么我使用上述方法.. – 2011-04-30 06:41:48