1

I have a grid which contains a number of textboxes. I want to move from one text box to other using up/down/left/right keys. I have tried setting KeyBoardNavigation.DirectionNavigation property on the grid but it doesn't work. However the Tab navigation is working fine. Any suggestion please.

indra
  • 145
  • 3
  • 11

1 Answers1

4

As I said in my comment: this is a strange thing to want to do - however, I think the easiest way is to make yourself a special jumpy textbox which overrides the preview keydown event to action the focus change.

public class JumpyTextBox : TextBox
{
    protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        base.OnPreviewKeyDown(e);

        if (e.Key == Key.Up || e.Key == Key.Down)
            MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));

        if (e.Key == Key.Down || e.Key == Key.Right)
            MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }
}

The XAML then works like a normal textbox:

<local:JumpyTextBox Grid.Column="0" Text="field 1" />
<local:JumpyTextBox Grid.Column="1" Text="field 2" />

You can then contain the tab navigation inside your grid with the 'special' textboxes:

<Grid KeyboardNavigation.TabNavigation="Cycle">
James Harcourt
  • 6,017
  • 4
  • 22
  • 42