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.
Asked
Active
Viewed 5,475 times
1
-
Seems mad to take away the right and left arrow key function from a text box. The user will now have to use the mouse to position the cursor? – James Harcourt Jun 07 '15 at 16:18
-
Yes they have to use mouse for that. The functionality will be similar to excel. Any idea how to implement it?? – indra Jun 07 '15 at 16:39
-
See my answer below - just edited. – James Harcourt Jun 07 '15 at 16:43
1 Answers
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