WPF 剪贴板操作期间数据丢失
我正在 WPF RichTextBox
中使用 FlowDocument
。某些流文档元素是使用 System.Windows.Documents 框架类的子类创建的,所有元素都使用 Tag 属性来存储附加数据。
如果我使用 XamlWriter 序列化文档树,所有内容都会正确反映在生成的 Xaml
输出中。
但是,如果我只是在 RichTextBox
中进行复制和粘贴,尽管粘贴的元素在视觉上与复制它们的元素相同,但剪贴板操作会丢弃所有附加数据。具体来说,所有子类元素都被粘贴为其基本框架类型的实例,并且它们的 Tag
属性中没有数据。
这表明 RichTextBox
上的 WPF 剪贴板操作不使用 XamlWriter
进行序列化,尽管序列化的剪贴板数据将其格式标识为“Xaml”。
我认为这种行为的原因是为了确保在粘贴到其他不一定了解我的自定义类型的 Xaml 感知应用程序时有一个共同点。但我需要实现更丰富的复制/粘贴机制以在我的应用程序中使用。
我想我可能可以拦截复制事件并以自定义格式添加剪贴板数据,随后将其应用到粘贴事件中。然而,这带来了其自身的复杂性,因为元素可能需要在粘贴之前进行包装(例如,粘贴到块元素上下文中的内联元素)。
因此,我希望避免重新发明轮子,并且希望得到有关如何使用现有框架基础设施使其工作的任何建议。
I am working with a FlowDocument
in a WPF RichTextBox
. Some of the flow document elements are created using subclasses of the System.Windows.Documents
framework classes and all of the elements use the Tag
property to store additional data.
If I use a XamlWriter to serialize a document tree, everything is correctly reflected in the resulting Xaml
output.
However, if I simply copy and paste within the RichTextBox
, although the pasted elements are visually identical to those from which they were copied, the clipboard operation discards all my additional data. Specifically, all the subclassed elements are pasted as instances of their base framework types and none of them have data in their Tag
property.
This suggests that a WPF clipboard operation on a RichTextBox
does not use XamlWriter
for serialization, despite the fact that the serialized clipboard data identifies its format as "Xaml".
I imagine that the reason for this behavior is to ensure a common denominator when pasting into other Xaml-aware applications that do not necessarily have knowledge of my custom types. But I need to implement a richer copy / paste mechanism for use within my application.
I guess I can probably intercept the copy event and add clipboard data in a custom format, which is subsequently applied in the paste event. However, this presents its own complications, as elements may need to be wrapped before pasting (for example inline elements that are pasted into a block element context).
So, I am hoping to avoid reinventing the wheel and would appreciate any advice on how to get this to work using the existing framework infrastructure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对你的问题没有确切的答案,但我可以想出两种方法来继续 -
关联一个
CommandBinding
复制/粘贴操作,与您
控制并执行自定义
Execute
和CanExecute
中的操作方法。正如这里所解释的:
<块引用>
http://msdn.microsoft.com /en-us/library/system.windows.input.commandbinding.aspx
使用
DataObject
类进行拦截复制/粘贴事件并执行
您在那里的自定义操作。 IE
DataObject.AddCopyingHandler
和
DataObject.AddPastingHandler
.一些有用的链接 -
希望能有所帮助。
I don't have an exact answer to your question but I can think of two ways to proceed with -
Associate a
CommandBinding
forCopy/Paste operation, with your
controls and perform custom
operations in
Execute
andCanExecute
methods. As explained here:
Use
DataObject
class to interceptthe Copy/Paste events and perform
your custom operation there. i.e.
DataObject.AddCopyingHandler
and
DataObject.AddPastingHandler
.Some useful links -
Hope that will help.