WPF 剪贴板操作期间数据丢失

发布于 2024-09-10 12:35:33 字数 715 浏览 1 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

·深蓝 2024-09-17 12:35:33

我对你的问题没有确切的答案,但我可以想出两种方法来继续 -

  1. 关联一个 CommandBinding
    复制/粘贴操作,与您
    控制并执行自定义
    ExecuteCanExecute 中的操作
    方法。正如这里所解释的:

    <块引用>

    http://msdn.microsoft.com /en-us/library/system.windows.input.commandbinding.aspx

  2. 使用DataObject类进行拦截
    复制/粘贴事件并执行
    您在那里的自定义操作。 IE
    DataObject.AddCopyingHandler

    DataObject.AddPastingHandler.


一些有用的链接 -

http://msdn.microsoft.com/en -us/library/system.windows.dataobject_members.aspx

链接

希望能有所帮助。

I don't have an exact answer to your question but I can think of two ways to proceed with -

  1. Associate a CommandBinding for
    Copy/Paste operation, with your
    controls and perform custom
    operations in Execute and CanExecute
    methods. As explained here:

    http://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.aspx

  2. Use DataObject class to intercept
    the Copy/Paste events and perform
    your custom operation there. i.e.
    DataObject.AddCopyingHandler
    and
    DataObject.AddPastingHandler.

Some useful links -

http://msdn.microsoft.com/en-us/library/system.windows.dataobject_members.aspx

Link

Hope that will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文