wpf 如何通过箭头键禁用焦点更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24447602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:01:23  来源:igfitidea点击:

How to disable focus changes by arrow keys

c#wpfwpf-controlsfocusarrow-keys

提问by xelor

I have a WPF window with a few controls (buttons, groupboxes, etc) and one big Viewport3Dwithin a Border.

我有几个控件(按钮,groupboxes等)和一个大的WPF窗口Viewport3DBorder

The viewport shows a 3D scene and I want the arrow keys to move its camera around. The problem: the arrow keys always change the focus to another UIElement.

视口显示了一个 3D 场景,我希望箭头键可以四处移动它的相机。问题:箭头键总是将焦点更改为另一个UIElement

How can I disable focus changes by the arrow keys and have them change the camera position instead?

如何通过箭头键禁用焦点更改并让它们更改相机位置?

采纳答案by Sheridan

It always difficult to answer without having some code to actually test, because without it, we are just guessing really. Either way, I can't comment on your particular situation, but in general, if we want to stop some pre-defined action from happening in an event, then we typically handle that event and set the e.Handledproperty to true.

如果没有一些代码来实际测试,总是很难回答,因为没有它,我们只是在猜测。无论哪种方式,我都无法对您的特定情况发表评论,但总的来说,如果我们想阻止某些预定义的操作在事件中发生,那么我们通常会处理该事件并将e.Handled属性设置为true.

Seeing as you already want to handle the KeyDownevent to detect the use of the arrow keys, then you could set the e.Handledproperty to trueat the same time. However, you should handle the PreviewKeyDownevent instead, because it occurs beforethe KeyDownevent and so is more likely to have the effect that you want. Try something like this:

既然您已经想要处理KeyDown事件以检测箭头键的使用,那么您可以同时将该e.Handled属性设置为true。但是,你应该处理的PreviewKeyDown事件,而不是,因为它发生之前KeyDown事件,因此更可能有你想要的效果。尝试这样的事情:

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Left || e.Key == Key.Right)
    {
        // Move your camera here
        e.Handled = true;
    }
}

回答by Jonathan Allan

If we want to be able to tab between controls but not arrow key through the same set of controls once they are in keyboard navigation mode it should be that we can set KeyboardNavigation.DirectionalNavigation="None"for the container (in this case the window).

如果我们希望能够在控件之间切换,但不能在同一组控件处于键盘导航模式时使用箭头键,那么我们应该可以KeyboardNavigation.DirectionalNavigation="None"为容器(在本例中为窗口)设置。

However it seems there is some bug stopping that solution from working.

然而,似乎有一些错误阻止了该解决方案的工作。

The workaround of setting KeyboardNavigation.DirectionalNavigation="Once"works though.

设置的解决方法KeyboardNavigation.DirectionalNavigation="Once"虽然有效。

  • The only caveat I've noticed so far is that upon pressing an arrow key the FocusVisualStyleis reloaded for the currently selected element.
  • 到目前为止,我注意到的唯一警告是,在按下箭头键时,FocusVisualStyle会为当前选定的元素重新加载。

回答by Ashok Rathod

You can try

你可以试试

KeyboardNavigation.TabNavigation = "Local"

for not shifting your focus outside the Viewport3D.

因为没有将注意力转移到Viewport3D.