WPF:KeyboardNavigationMode.Contained 无法抑制焦点换行

发布于 2024-08-04 18:46:41 字数 613 浏览 1 评论 0原文

我想用箭头键在窗口中导航。到目前为止它可以工作,但如果我到达窗口的末尾,焦点就会回到第一个主菜单项。但我希望焦点停止在窗口中的最后一个控件上。

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
   if (e.Key == Key.Down)
   {
     elem.MoveFocus(FocusNavigationDirection.Next);
   }
}

“elem”是“stackPanel”的间接子控件。 MoveFocus 总是返回 true,我已经尝试过: KeyboardNavigation.SetTabNavigation(stackPanel, KeyboardNavigationMode.Contained); KeyboardNavigation.SetDirectionalNavigation(stackPanel,KeyboardNavigationMode.Contained); KeyboardNavigation.SetControlTabNavigation(stackPanel, KeyboardNavigationMode.Contained);

没有任何帮助。

I want to navigate in my window with the arrow key. It works so far but if I reach the end of my window, focus wraps to the first main menu item. But I want that focus stops at the last control in my window.

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
   if (e.Key == Key.Down)
   {
     elem.MoveFocus(FocusNavigationDirection.Next);
   }
}

"elem" is indirect child control of "stackPanel". MoveFocus always returns true and I already tried:
KeyboardNavigation.SetTabNavigation(stackPanel, KeyboardNavigationMode.Contained); KeyboardNavigation.SetDirectionalNavigation(stackPanel,KeyboardNavigationMode.Contained);
KeyboardNavigation.SetControlTabNavigation(stackPanel, KeyboardNavigationMode.Contained);

Nothing helped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2024-08-11 18:46:42

使用 TraversalRequest 实例怎么样?

if (e.Key == Key.Down)
{
    e.Handled = true;
    elem.MoveFocus(new TraversalRequest 
    { 
        FocusNavigationDirection = FocusNavigationDirection.Next,
        Wrapped = false
    });
}

请注意,TraversalRequest.Wrapped 的 MSDN 文档 表示默认情况下它是 false,因此焦点无论如何都不应该换行。

How about using a TraversalRequest instance?

if (e.Key == Key.Down)
{
    e.Handled = true;
    elem.MoveFocus(new TraversalRequest 
    { 
        FocusNavigationDirection = FocusNavigationDirection.Next,
        Wrapped = false
    });
}

Mind you, the MSDN documentation for TraversalRequest.Wrapped says that it's false by default, so the focus shouldn't wrap anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文