public static DataGridCell GetDataGridCell(DataGrid dataGrid, DataGridRow rowContainer, int column) { if (rowContainer != null) { DataGridCellsPresenter presenter = WpfTools.FindVisualChild <DataGridCellsPresenter>(rowContainer); if (presenter == null) { /* if the row has been virtualized away, call its ApplyTemplate() method * to build its visual tree in order for the DataGridCellsPresenter * and the DataGridCells to be created */ rowContainer.ApplyTemplate(); presenter = WpfTools.FindVisualChild <DataGridCellsPresenter>(rowContainer); } if (presenter != null) { DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell; if (cell == null) { /* bring the column into view * in case it has been virtualized away */ dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]); cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell; } return(cell); } } return(null); }
public static void SetFocusOnNewCreatedColumn(DataGrid DataGrid, int rowIndex) { DataGrid.Focus(); DataGridRow rowContainer = DataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; if (rowContainer == null) { DataGrid.SelectedIndex = rowIndex; DataGrid.ScrollIntoView(DataGrid.SelectedItem); rowContainer = DataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; } if (rowContainer != null) { rowContainer.ApplyTemplate(); DataGridCellsPresenter presenter = WpfTools.FindVisualChild <DataGridCellsPresenter>(rowContainer); DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell; if (cell == null) { /* bring the column into view in case it has been virtualized away */ DataGrid.ScrollIntoView(rowContainer, DataGrid.Columns[0]); cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell; } if (cell != null) { cell.Focus(); } } }