public DataGridCell GetCell(DataGridRow rowContainer, int column) { if (rowContainer != null) { DataGridCellsPresenter presenter = 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 = 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 */ GcodeGrid.ScrollIntoView(rowContainer, GcodeGrid.Columns[column]); cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell; } return(cell); } } return(null); }
//https://social.technet.microsoft.com/wiki/contents/articles/21202.wpf-programmatically-selecting-and-focusing-a-row-or-cell-in-a-datagrid.aspx public async void SelectRowAsync(GcodeLine _gcodeline) { await Task.Run(() => Dispatcher.BeginInvoke(new Action(delegate() { // if (_gcodeline.InSerialBuffer) { // GcodeGrid.SelectedItems.Clear(); object item = GcodeGrid.Items[_gcodeline.Index]; GcodeGrid.SelectedItem = item; if (App.Grbl.InRunState) { GcodeGrid.ScrollIntoView(item); } //if (!(GcodeGrid.ItemContainerGenerator.ContainerFromIndex(_gcodeline.Index) is DataGridRow row)) //{ // GcodeGrid.ScrollIntoView(item); // row = GcodeGrid.ItemContainerGenerator.ContainerFromIndex(_gcodeline.Index) as DataGridRow; //} ////row.Focus(); //DataGridCell cell = GetCell(row, 2); //if (cell != null) //{ // cell.Focus(); //} } }))); }