2

私は単純なアプリケーション (テスト用に 6 つのボタン (3 つの行が 2 行) を持つグリッド) を持っており、左矢印キーと右矢印キーを次のように処理しています。

    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 で指定された方向とは反対の方向に移動するように見えるため、これは期待どおりに動作しません。

なぜこれになるのかについて何か考えはありますか?MSDNのドキュメントでは、「の左側」がどのように定義されているかについて、少しあいまいです。

必要に応じて、各ボタンのタブ ストップを 1 ~ 6 として定義しました。

4

2 に答える 2

3

Why do you need to get the elementWithFocus while you can use "this" (window own scope)?

I modified your code a bit and it worked for me:

    private void Window_OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        switch (e.Key)
        {
            case Key.Left:
                this.MoveFocus(FocusNavigationDirection.Previous);
                break;
            case Key.Right:
                this.MoveFocus(FocusNavigationDirection.Next);
                break;
            default:
                break;
        }
    }

    private void MoveFocus(FocusNavigationDirection direction)
    {
        var request = new TraversalRequest(direction);
        this.MoveFocus(request);
    }
于 2013-02-01T11:16:36.897 に答える
1

FocusNavigationDirection.Previous(左用) と(右用)を試しましたFocusNavigationDirection.Nextか?

これにより、他の値よりも予測可能な動作が得られる場合があります。

于 2011-07-13T16:56:06.753 に答える