问题
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