WPF detect key sequence

南笙酒味 提交于 2019-12-24 20:54:12

问题


I would like to hide a groupox from my WPF app and to manually enable it when I need it via a keypress code when app is active(not minimized)

this example works sometimes with just one keypress, but I need a sequence

protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        base.OnPreviewKeyDown(e);
        if (e.Key == Key.B)
        {
            bdgb.Visibility = Visibility.Visible;
        }
    }

回答1:


You could for example override the OnPreviewKeyDown method of your window, e.g.:

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
    base.OnPreviewKeyDown(e);
    if (e.Key == Key.B)
    {
        //...
    }
}



回答2:


On what did you put the handler? Only currently focused element gets the notification, and if it is not handled, it goes up the tree.

You need to put it in the most top UIElement, which means your window.

Or, you can do it in a moer MVVMy WPF style, by creating a command binding to the key and having a property on your VM GroupBoxVisible, set it to "True" and have binding to the GroupBox.Visibillity.

Moer details here



来源:https://stackoverflow.com/questions/54111705/wpf-detect-key-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!