2011-04-29 37 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