私は単純なアプリケーション (テスト用に 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 として定義しました。