在最后一个单元格上进行选项卡时,WPF DataGrid失去焦点

时间:2012-11-02 18:01:28

标签: wpf datagrid

我的窗口中有一些按钮和一个数据网格。

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

当我编辑NewItemPlaceholder-row时,在更改第一个值(但没有新的NewItemPlaceholder-row)时会创建新项目。当我编辑最后一个值并继续使用Tab键时,会生成一个新行。 但是光标移动到Save Button而不是新行中的第一个单元格。

如何将注意力集中在网格上?

为了完整性:我使用ObservableCollection作为ItemsSource。

3 个答案:

答案 0 :(得分:3)

使用KeyboardNavigation.TabNavigation附加属性:

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

答案 1 :(得分:1)

我在这里找到了一个解决方案: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 });
        }
    }
}

使用此处的GetCell和GetRow方法: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;
    }
}

答案 2 :(得分:0)

Here is an alternative打包后,您可以使用DataGrid的默认样式启用它,并在整个应用程序中应用它。