开发者

Shortcut keys not working after messagebox is shown

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开发者_C百科 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>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜