Christian80
Christian80

Reputation: 459

How to set focus on a row in grid in WPF

I have an edit window which pop up with a Grid. This grid is a C1FlexGrid made by ComponentOne. (but I think that this might be a similar problem with other grids..)

I want the first row in that grid to get focus and be selected when I open that window from the main window.

Preferably in xaml, but if that is not possible in the codebehind or by inheriting the flexgrid.

I know how to set a row to be selected and the grid to get focus but the row isn’t focused so I can’t traverse the rows in the grid by moving with the up and down arrows on the keyboard.

Upvotes: 3

Views: 2234

Answers (1)

ravi
ravi

Reputation: 103

Programmatically select a row within the WPF DataGrid and then programmatically put the focus on that cell, so that you can then navigate through the DataGrid with the arrow keys on the keyboard, Part of the code for selecting would be:

int index = 1;
dgUsers.SelectedItem = dgUsers.Items[index];
dgUsers.ScrollIntoView(dgUsers.Items[index]);
DataGrid dgrow =                                                                                                 (DataGrid)dgUsers.ItemContainerGenerator.ContainerFromItem(dgUsers.Items[index]);
dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

Upvotes: 1

Related Questions