public TreeViewGrid() { InitializeComponent(); // Create a list of items to put in the view (will be populated in SetController() call. m_flattenedTree = new ObservableCollectionEx <TreeViewGridNode>(); Grid.ItemsSource = m_flattenedTree; // Make the name column have tree control behavior var nameColumn = Grid.Columns[0] as DataGridTemplateColumn; Debug.Assert(nameColumn != null); var template = (DataTemplate)Resources["TreeControlCell"]; Debug.Assert(template != null); nameColumn.CellTemplate = template; // Put the indentation in when we cut and paste nameColumn.ClipboardContentBinding = new Binding("IndentedName"); Grid.CopyingRowClipboardContent += delegate(object sender, DataGridRowClipboardEventArgs e) { for (int i = 0; i < e.ClipboardRowContent.Count; i++) { var clipboardContent = e.ClipboardRowContent[i]; string morphedContent = null; if (e.IsColumnHeadersRow) { morphedContent = GetColumnHeaderText(clipboardContent.Column); } else { var cellContent = clipboardContent.Content; if (cellContent is float) { morphedContent = GoodPrecision((float)cellContent, clipboardContent.Column); } else if (cellContent is double) { morphedContent = GoodPrecision((double)cellContent, clipboardContent.Column); } else if (cellContent != null) { morphedContent = cellContent.ToString(); } else { morphedContent = ""; } morphedContent = CompressContent(morphedContent); } if (e.ClipboardRowContent.Count > 1) { morphedContent = PadForColumn(morphedContent, i + e.StartColumnDisplayIndex); } // TODO Ugly, morph two cells on different rows into one line for the correct cut/paste experience // for ranges. if (m_clipboardRangeEnd != m_clipboardRangeStart) // If we have just 2 things selected (and I can tell them apart) { if (VeryClose(morphedContent, m_clipboardRangeStart)) { e.ClipboardRowContent.Clear(); morphedContent = morphedContent + " " + m_clipboardRangeEnd; e.ClipboardRowContent.Add(new DataGridClipboardCellContent(clipboardContent.Item, clipboardContent.Column, morphedContent)); return; } else if (VeryClose(morphedContent, m_clipboardRangeEnd)) { e.ClipboardRowContent.Clear(); return; } } e.ClipboardRowContent[i] = new DataGridClipboardCellContent(clipboardContent.Item, clipboardContent.Column, morphedContent); } }; }