如何在WPF中获取剪贴板内容 [英] How to get the clipboard content in WPF
本文介绍了如何在WPF中获取剪贴板内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个应用程序,当用户在Datagrid中选择行并按Ctrl C时,它必须获取剪贴板内容.按Ctrl V时必须粘贴.
我将模式设置为
Hi,
I am writing an application in which when user select rows in Datagrid and press Ctrl C, it has to get the clip board content. When Ctrl V is pressed it has to paste.
I am setting mode as
myDataGrid.ClipBoardCopyMode = Windows.Controls.DataGridCopyMode.ExcludeHeader
之后,我要获取剪贴板内容,如我们使用的Windows窗体中的
myDataGridView.GetClipboardContent()方法获取内容.
WPF中是否有类似于Windows Form的GetClipboardContent()的方法.
感谢任何建议,
Vijay
after this I want to get the clipboard content as in the windows form where we use
myDataGridView.GetClipboardContent() method to get the content.
Is there any method in WPF similar to GetClipboardContent() of Windows Form.
Appreciate any suggestions,
Vijay
推荐答案
您需要使用以下命令: ^ ].
您想要的方法是GetData
或GetDataObject
.
可能是您的问题正在使用自定义数据格式.在此剪贴板类中,此功能是通过特殊的数据格式预定义的,该数据格式由类DataFormats
及其公共字段DataFormats.Serializable
:
表示. http://msdn.microsoft.com/en-us/library/system.windows. dataformats.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.dataformats.serializable.aspx [ ^ ].
此格式的使用基于使用实现接口IDataObject
的类DataObject
的假设:
http://msdn.microsoft.com/en-us/library/system.windows. dataobject.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows. idataobject.aspx [ ^ ].
它的背景非常简单有趣.由于剪贴板的底层是一些序列化的原始字节数据(否则,剪贴板是系统范围的),因此该机制基于串行化:
http://en.wikipedia.org/wiki/Serialization [ http://en.wikipedia.org/wiki/Serialization#.NET_Framework [ http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx [ ^ ].
自定义剪贴板类型是用一些字符串 atom 注册的,该字符串在系统范围内是唯一的,应通过将数据放入剪贴板并读取的代码来识别.不论哪种类型,两个部分都使用相同的序列化机制就足够了.一旦代码部分共享相同的机制,一切就可以工作.对于不使用此自定义类型的应用程序,这是一些无法识别的格式,因此未订阅.
-SA
You need to use this: http://msdn.microsoft.com/en-us/library/system.windows.clipboard.aspx[^].
The method you want isGetData
orGetDataObject
.
Probably, your problem is working with the custom data format. In this clipboard class, this functionality is predefined via the special data format expressed by means of the classDataFormats
and its public fieldDataFormats.Serializable
:
http://msdn.microsoft.com/en-us/library/system.windows.dataformats.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.dataformats.serializable.aspx[^].
The use of this format is based on the assumption of using the classDataObject
implementing the interfaceIDataObject
:
http://msdn.microsoft.com/en-us/library/system.windows.dataobject.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.idataobject.aspx[^].
The background of it is pretty simple and interesting. As the low-level of the Clipboard is some serialized raw byte data (how else? Clipboard is system-wide), the mechanism is based on serialization:
http://en.wikipedia.org/wiki/Serialization[^],
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[^].
http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx[^].
Custom Clipboard types are registered with some string atom, unique in the scope of system which should be recognized by the code putting data in Clipboard and reading it. It''s enough that both parts used identical serialization mechanism, not matter what kind. As soon as those to parts of code share the same mechanism, everything works; and for the applications not involved in using this custom type, this is some unrecognized format they are not subscribed to.
—SA
要将DataGrid内容获取到剪贴板,我们应该使用Clipboard GetData方法.
下面的示例演示如何将DataGrid的所有单元格"myDataGrid"复制到文本框.
当复制模式选择为IncludeHeader时,它将复制DatGrid的ColumnName.
To get the DataGrid content to clipboard we should use the Clipboard GetData method.
Below example shows how to copy All the cells of DataGrid "myDataGrid" to textbox.
It will copy the ColumnName of DatGrid as IncludeHeader is selected for copy mode.
Me.myDataGrid.SelectAllCells()
Me.myDataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader
ApplicationCommands.Copy.Execute(Nothing, Me.myDataGrid)
Dim tempString as String = Clipboard.GetData(DataFormats.CommaSeperatedValue)
Me.myTextBox.text = tempString
这篇关于如何在WPF中获取剪贴板内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文