Reputation: 460
I have a simple data grid and I was using the down arrow key to browse through the items inside, but when I reached the last row and continued pressing down, it stopped and couldn't navigate anymore as if it has lost its focus. how can I loop through this using the keys? Is there a property for this in markup or do I need to do it in the code behind?
Upvotes: 0
Views: 1570
Reputation: 2469
As far as I know, there isn't a way to change the default selection behaviour from the markup, so you would need to do this in the code behind.
Default behaviour described here:
Default Keyboard and Mouse Behavior in the DataGrid Control
This should be a simple thing whereby you can just do something like this in the PreviewKeyDown event handler for the DataGrid:
if (e.Key == Key.Down && MyDataGrid.SelectedIndex == (MyDataGrid.Items.Count - 1))
{
MyDataGrid.SelectedIndex = 0;
MyDataGrid.ScrollIntoView(MyDataGrid.SelectedItem);
e.Handled = true;
}
However, although this will select the top row as you want, the keyboard focus of the selected cell will interfere with future key presses.
If you do really want this behaviour, there is a very good article here:
WPF: Programmatically Selecting and Focusing a Row or Cell in a DataGrid
Upvotes: 1