WPF DataGrid,Ctrl+C 后复制到剪贴板,OnCopyingRowClipboardContent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13876874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid, Copy to Clipboard after Ctrl+C,OnCopyingRowClipboardContent
提问by Alex N
For WPF, Data Grid I am trying to copy to clipboard my custom text data, after Ctrl+CDiverse attempts to use override
OnCopyingRowClipboardContent(DataGridRowClipboardEventArgs args)or CopingRowClipboardContent event, don't help.
对于 WPF,数据网格我试图将我的自定义文本数据复制到剪贴板,在Ctrl+CDiverse 尝试使用 override
OnCopyingRowClipboardContent(DataGridRowClipboardEventArgs args)或 后CopingRowClipboardContent event,没有帮助。
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();
}
s1gets desired text, but after going out of this method clipboard gets empty.
Any idea if one can solve this?
s1获取所需的文本,但退出此方法后剪贴板变空。知道有人可以解决这个问题吗?
回答by daniele3004
the correct way is add on XAML grid this property
正确的方法是在 XAML 网格上添加此属性
ClipboardCopyMode="ExcludeHeader"
and for each property you want copy add this XAML
并为您想要复制的每个属性添加此 XAML
<DataGridTemplateColumn ClipboardContentBinding="{Binding XXXXXX} .....
other facultative step is implement the dataGrid event CopyingRowClipboardContent to modify the clipoard data
另一个兼性步骤是实现 dataGrid 事件 CopyingRowClipboardContent 来修改剪贴板数据
回答by Dtex
You need to set the ClipboardRowContent property of DataGridRowClipboardEventArgs
您需要设置 DataGridRowClipboardEventArgs 的 ClipboardRowContent 属性
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"));
}

