Home > .NET, Silverlight > Coping contents of Template Column in data grid to clipboard

Coping contents of Template Column in data grid to clipboard

August 18th, 2010

There are some nice new features (coming from Silverlight 2) in the latest versions of Silverlight – being able to copy rows in a DataGrid via the familiar Control+C for instance.

But I noticed something strange: some columns were blank when I pasted them into Excel (or notepad). The blank columns were always DataGridTemplateColumn and that made sense. How could it know what to copy from one or more controls?

Turns out this is really easy to fix if you are using data binding. All you have to do is bind the ClipboardContentBinding property on the DataGridTemplateColumn with the value you want copied for this column.

For example:

1
2
3
4
5
6
7
<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>

And that’s it. Control+C copy works as desired.

.NET, Silverlight ,

  1. cyJFarmer
    August 25th, 2010 at 16:54 | #1

    Hey thanks. it works

  1. No trackbacks yet.
You must be logged in to post a comment.