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

无人久伴 提交于 2019-12-09 08:14:06

问题


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?


回答1:


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




回答2:


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"));
}


来源:https://stackoverflow.com/questions/13876874/wpf-datagrid-copy-to-clipboard-after-ctrlc-oncopyingrowclipboardcontent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!