2
votes

I have a WPF DataGrid with a bunch of DataGridTextColumn columns. Some of them have CellStyle property set with a custom Style with <DataTrigger> elements that set the Background color of the cells based on their value.

It appears selecting a row and pressing Ctrl-C only copies the cell text to the clipboard, not the colors. So pasting it to programs like OneNote produces a table with black text, and no background colors are copied.

Is there a way to enable copying of the style as well?

Thanks.

1

1 Answers

1
votes

Unfortunately you have to write something to do it yourself.

Here is the source for the code that handles ApplicationCommands.Copy for the DataGrid, and on line 8252 are all the formats for which the copy is implemented.

Collection<string> formats = new Collection<string>(new string[] { DataFormats.Html, DataFormats.Text, DataFormats.UnicodeText, DataFormats.CommaSeparatedValue });

and from visualstudio immediate window output we can see the result of ApplicationCommands.Copy on the DataGrid.

Clipboard.GetDataObject().GetFormats()
[0]: "CSV"
[1]: "Text"
[2]: "UnicodeText"
[3]: "System.String"
[4]: "HTML Format"

Out of those only HTML has any styling capabilities, but as of now the code only implements it to create an HTML Table.

You can however override the DataGrid.OnExecutedCopy and implement copy in a format that supports styling yourself.