views:

59

answers:

2

With Silverlight 4, I can select one or more cells (or rows and columns) in a DataGrid, hit Control+C and the contents are copied to the clipboard. Which is really cool. Upon Control+V, it can be pasted into Excel or some other editor.

However, the if one of the columns is a DataGridTemplateColumn it's values are blank when pasted. Which makes sense, because it could be anything in the column.

How can I tell the Control+C copy what the value of the template cell should be?

A: 

If you catch the event CopyingRowClipboardContent on the DataGrid, you get DataGridRowClipboardEventArgs that contain the Databound item in ClipboardRowContent.

var dataGridClipboardCellContent = new List<DataGridClipboardCellContent>();
foreach (var item in e.ClipboardRowContent)
{
    dataClipboardCellContent.Add(new DataGridClipboardCellContent(item, item.Column, "some value"));
}
e.ClipboardRowContent.Clear();
e.ClipboardRowContent.AddRange(dataClipboardCellContent);
Truls Clauss
+1  A: 

Turns out this is really easy if you are using data binding. All you have to do is bind the

ClipboardContentBinding
property with the value you want copied for this column.

For example:

<data:DataGridTemplateColumn Header="Name" ClipboardContentBinding="{Binding Name}" SortMemberPath="Name">
  <data:DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
        <HyperlinkButton Content="{Binding Name}" Margin="3" />
     </DataTemplate>
  </data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
Ben Farmer