public static void SelectCellsByIndexes(System.Windows.Controls.DataGrid dataGrid, IList <KeyValuePair <int, int> > cellIndexes) { if (!dataGrid.SelectionUnit.Equals(System.Windows.Controls.DataGridSelectionUnit.Cell)) { throw new ArgumentException("The SelectionUnit of the DataGrid must be set to Cell."); } if (!dataGrid.SelectionMode.Equals(System.Windows.Controls.DataGridSelectionMode.Extended)) { throw new ArgumentException("The SelectionMode of the DataGrid must be set to Extended."); } dataGrid.SelectedCells.Clear(); foreach (KeyValuePair <int, int> cellIndex in cellIndexes) { int rowIndex = cellIndex.Key; int columnIndex = cellIndex.Value; if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1)) { throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex)); } if (columnIndex < 0 || columnIndex > (dataGrid.Columns.Count - 1)) { throw new ArgumentException(string.Format("{0} is an invalid column index.", columnIndex)); } object item = dataGrid.Items[rowIndex]; //= Product X System.Windows.Controls.DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as System.Windows.Controls.DataGridRow; if (row == null) { dataGrid.ScrollIntoView(item); row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as System.Windows.Controls.DataGridRow; } if (row != null) { System.Windows.Controls.DataGridCell cell = GetCell(dataGrid, row, columnIndex); if (cell != null) { System.Windows.Controls.DataGridCellInfo dataGridCellInfo = new System.Windows.Controls.DataGridCellInfo(cell); dataGrid.SelectedCells.Add(dataGridCellInfo); cell.Focus(); } } } }
private void ExportDataItemCore(FilterDataGrid filterDataGrid, ClipboardExporterBase clipboardExporter, object item, System.Windows.Controls.DataGridCellInfo[] cells, DataGridWpfColumnViewModel[] wpfColumns, System.Windows.Controls.DataGridColumn[] dataGridColumns) { clipboardExporter.StartDataItem(filterDataGrid, item); // Ensure the count does not exceeds the columns count int columnsCount = wpfColumns.Length; Type itemType = item.GetType(); for (int i = 0; i < columnsCount; i++) { DataGridWpfColumnViewModel wpfColumn = wpfColumns[i]; System.Windows.Controls.DataGridColumn dataGidColumn = dataGridColumns[i]; System.Windows.Controls.DataGridCellInfo dataGridCellInfo = cells[i]; (object rawValue, object formattedValue)field = (default, default);
public void AddTask(System.Windows.Controls.DataGrid tasksGrid) { //GetTimeRemaining() SelectedTask = InternalAddTask(String.Format("Task {0}", tasks.Count + 1), ColorInfo.GetRandomColor(), new TimeInfoViewModel()); if (tasksGrid != null) { tasksGrid.ScrollIntoView(selectedTask); var cellInfo = new System.Windows.Controls.DataGridCellInfo(selectedTask, tasksGrid.Columns[1]); int row = tasksGrid.Items.IndexOf(selectedTask); tasksGrid.CurrentCell = cellInfo; var cell = tasksGrid.GetCell(row, 1); if (cell != null) { cell.Focus(); } tasksGrid.BeginEdit(); } NotifyOfPropertyChange(() => CanAddTask); NotifyOfPropertyChange(() => CanStartTasks); NotifyOfPropertyChange(() => CanAddTasks); CalculateValidationErrors(); }