WPF FocusNavigationDirection,MoveFocus和箭头键 [英] WPF FocusNavigationDirection, MoveFocus and Arrow keys

查看:1458
本文介绍了WPF FocusNavigationDirection,MoveFocus和箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序(使用6个按钮的网格 - 2排3 - 它进行测试)和正在处理左,右方向键如下:

I have a simple application (a grid with 6 buttons - 2 rows of 3 - on it for testing) and am handling left and right arrow keys as follows

    private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        FocusNavigationDirection focusDirection = new System.Windows.Input.FocusNavigationDirection();

        switch (e.Key)
        {
            case Key.Left:
                focusDirection = System.Windows.Input.FocusNavigationDirection.Left;
                break;
            case Key.Right:
                focusDirection = System.Windows.Input.FocusNavigationDirection.Right;
                break;
            default:
                break;
        }
        TraversalRequest request = new TraversalRequest(focusDirection);

        // Gets the element with keyboard focus.
        UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;

        // Change keyboard focus.
        if (elementWithFocus != null)
        {
            elementWithFocus.MoveFocus(request);
        }

    }



不幸的是,这并不表现如我期望为重点似乎总是在相反的方向上,通过该FocusNavigationDirection指定移动。

Unfortunately, this doesn't behave as I expect as the focus always seems to move in the opposite direction to that specified by the FocusNavigationDirection.

为什么这会是什么想法?该的 MSDN文档是一个有点模糊如何左的定义。

Any thoughts on why this would be? The MSDN Documentation is a bit vague on how "to the left of" is defined.

在情况下,它是需要的,我也通过6定义每个按钮为1的制表位。

In case it is needed, I have also defined the tab stops of each of the buttons as 1 through 6.

推荐答案

你试过 FocusNavigationDirection.Previous (左)和 FocusNavigationDirection.Next (右)?

Have you tried FocusNavigationDirection.Previous (for left) and FocusNavigationDirection.Next (for right)?

这可能会比其他值更可预测的行为。

That may give more predictable behavior than the other values.

这篇关于WPF FocusNavigationDirection,MoveFocus和箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆