Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid, Copy to Clipboard after Ctrl+C,OnCopyingRowClipboardContent

For WPF, Data Grid I am trying to copy to clipboard my custom text data, after Ctrl+C Diverse attempts to use override OnCopyingRowClipboardContent(DataGridRowClipboardEventArgs args) or CopingRowClipboardContent event, don't help.

Either clipboard gets empty or standard row text, but not what I would like to put there. For instance

protected override void OnCopyingRowClipboardContent(DataGridRowClipboardEventArgs args)
{
    Clipboard.SetText("Abc-hello");
    bool b1 = Clipboard.ContainsText();
    string s1 = Clipboard.GetText();
}

s1 gets desired text, but after going out of this method clipboard gets empty. Any idea if one can solve this?

like image 278
Alex N Avatar asked Dec 14 '12 10:12

Alex N


2 Answers

the correct way is add on XAML grid this property

ClipboardCopyMode="ExcludeHeader"

and for each property you want copy add this XAML

 <DataGridTemplateColumn  ClipboardContentBinding="{Binding XXXXXX} ..... 

other facultative step is implement the dataGrid event CopyingRowClipboardContent to modify the clipoard data

like image 59
daniele3004 Avatar answered Nov 17 '22 13:11

daniele3004


You need to set the ClipboardRowContent property of DataGridRowClipboardEventArgs

static void dataGrid_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
{
    e.ClipboardRowContent.Clear();
    e.ClipboardRowContent.Add(new DataGridClipboardCellContent(e.Item, (sender as DataGrid).Columns[0], "Abc-hello"));
}
like image 3
Dtex Avatar answered Nov 17 '22 14:11

Dtex