0
votes

I have some buttons and a datagrid inside my window.

<Button>Save</Button>
<Button>Back</Button>
<DataGrid x:Name="data" ItemsSource="{Binding Scores}" />

When I edit the NewItemPlaceholder-row, the new item is created when changing the first value (but no new NewItemPlaceholder-row). When I edit the last value and move on with Tab-key, a new row is generated. But the cursor moves to Save Button instead of the first cell in that new row.

How can I keep the focus on the grid?

For completeness: I am using an ObservableCollection as ItemsSource.

3

3 Answers

4
votes

Use the KeyboardNavigation.TabNavigation attached property:

<DataGrid x:Name="data" 
          ItemsSource="{Binding Scores}" 
          KeyboardNavigation.TabNavigation="Cycle" />
1
votes

I found a solution here: Customize focus behavior after a row commit through the DataGrid.RowEditEnding event

private void data_RowEditEnding_1(object sender, DataGridRowEditEndingEventArgs e) {
    if (e.EditAction == DataGridEditAction.Commit) {
        if (e.Row.Item == data.Items[data.Items.Count - 2]) {
            var rowToSelect = data.Items[data.Items.Count - 1];
            int rowIndex = data.Items.IndexOf(rowToSelect);
            this.Dispatcher.BeginInvoke(new DispatcherOperationCallback((param) => {
                var cell = DataGridHelper.GetCell(data, rowIndex, 0);
                cell.Focus();
                data.BeginEdit();
                return null;
            }), DispatcherPriority.Background, new object[] { null });
        }
    }
}

Using the GetCell and GetRow methods from here: datagrid get cell index

static class DataGridHelper {
    static public DataGridCell GetCell(DataGrid dg, int row, int column) {
        DataGridRow rowContainer = GetRow(dg, row);

        if (rowContainer != null) {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

            // try to get the cell but it may possibly be virtualized
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null) {
                // now try to bring into view and retreive the cell
                dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
        return null;
    }

    static public DataGridRow GetRow(DataGrid dg, int index) {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null) {
            // may be virtualized, bring into view and try again
            dg.ScrollIntoView(dg.Items[index]);
            row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

    static T GetVisualChild<T>(Visual parent) where T : Visual {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++) {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null) {
                child = GetVisualChild<T>(v);
            }
            if (child != null) {
                break;
            }
        }
        return child;
    }
}
0
votes

Here is an alternative packaged in such a way that you can enable it in the default style for DataGrid and apply it throughout your application.