private void OnKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) { // DataObject d = ClipboardHelper.ParseClipboardData(); Clipboard.SetDataObject(d); } if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) { // 2-dim array containing clipboard data string[][] clipboardData = ((string)Clipboard.GetData(DataFormats.Text)).Split('\n') .Select(row => row.Split('\t') .Select(cell => cell.Length > 0 && cell[cell.Length - 1] == '\r' ? cell.Substring(0, cell.Length - 1) : cell).ToArray()) .Where(a => a.Any(b => b.Length > 0)).ToArray(); // the index of the first DataGridRow int startRow = ItemContainerGenerator.IndexFromContainer( (DataGridRow)ItemContainerGenerator.ContainerFromItem(SelectedCells[0].Item)); int targetRowCount = SelectedCells.Count; // the destination rows // (from startRow to either end or length of clipboard rows) DataGridRow[] rows = Enumerable.Range( startRow, Math.Min(Items.Count, targetRowCount)) .Select(rowIndex => ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow) .Where(a => a != null).ToArray(); // the destination columns // (from selected row to either end or max. length of clipboard colums) DataGridColumn[] columns = Columns.OrderBy(column => column.DisplayIndex) .SkipWhile(column => column != this.CurrentCell.Column) .Take(clipboardData.Max(row => row.Length)).ToArray(); for (int rowIndex = 0; rowIndex < rows.Length; rowIndex++) { string[] rowContent = clipboardData[rowIndex % clipboardData.Length]; for (int colIndex = 0; colIndex < columns.Length; colIndex++) { string cellContent = colIndex >= rowContent.Length ? "" : rowContent[colIndex]; columns[colIndex].OnPastingCellClipboardContent( rows[rowIndex].Item, cellContent); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)