简体   繁体   English

在WPF中复制粘贴时如何不保存颜色?

[英]How to not save color when copy pasting in WPF?

Recently I encountered a problem with my application: when I copy paste the text out of my FlowDocument , it saves the color! 最近,我的应用程序遇到了一个问题:当我从FlowDocument复制粘贴文本时,它将保存颜色! This is an issue because other applications (like Microsoft Lync and Outlook) accept this color when pasting! 这是一个问题,因为其他应用程序(例如Microsoft Lync和Outlook)在粘贴时会接受这种颜色! My application uses a white font, which won't even show up on the white backgrounds in those applications. 我的应用程序使用白色字体,这些字体甚至不会显示在这些应用程序的白色背景上。 What really surprised me is that this isn't the case with the TextBox class. 真正令我惊讶的是TextBox类并非如此。 Copy pasting out of a TextBox does not preserve color. 复制粘贴到TextBox不会保留颜色。

Obviously I can just change the color of my text, but I like the color scheme and would like to keep it. 显然,我可以更改文本的颜色,但是我喜欢配色方案并希望保留它。 Is there a way I can disable copying the color out of the FlowDocument like in a TextBox ? 有没有一种方法可以像在TextBox一样禁止从FlowDocument复制颜色?

PS Please do not post answers telling me to use a TextBox instead of a FlowDocument . PS:请不要发布答案,告诉我使用TextBox而不是FlowDocument There's a reason I'm using those classes in their respective places, and I cannot interchange them. 我在各自的位置使用这些类是有原因的,我无法互换它们。

You can do: 你可以做:

public MainWindow()
{
    InitializeComponent();

    DataObject.AddCopyingHandler(flowDocumentViewer, OnCopy);
}

private void OnCopy(object sender, DataObjectEventArgs e)
{
    e.CancelCommand();

    Clipboard.SetText(flowDocumentViewer.Selection.Text);
}

Assuming that your FlowDocument is hosted in a FlowDocumentScrollViewer like this: 假设您的FlowDocument托管在FlowDocumentScrollViewer中,如下所示:

<FlowDocumentScrollViewer Name="flowDocumentViewer">
    <FlowDocument>

Best way is to create a custom copy Command , in which you can implement the code for getting just the text part of what you have selected in FlowDocument and then copy it to clip board. 最好的方法是创建一个自定义复制Command ,您可以在其中实现代码以仅获取在FlowDocument选择的文本部分,然后将其复制到剪贴板。 So that when you will paste it anywhere you will get just the text part what you have on clip board, not the formatting. 这样,当您将其粘贴到任何地方时,您只会得到剪贴板上的文本部分,而不是格式。 Provide a different KeyInputBinding for this command eg Ctrl+Shift+c, so that it won't conflict with the default copy functionality. 为此命令提供一个不同的KeyInputBinding ,例如Ctrl + Shift + c,这样它就不会与默认的复制功能冲突。

Even you can provide both options to the user if he wants to copy text with formatting or without formatting. 如果用户想复制带格式的文本或不带格式的文本,甚至可以为用户提供这两个选项。 Like when user choose to copy plain text call your custom Command and if user choose to copy formatted text call system copy Command (which by default get called when user do a Ctrl+c). 就像当用户选择复制纯文本时调用您的自定义Command以及如果用户选择复制格式化的文本时调用系统复制Command (默认情况下,当用户执行Ctrl + c时会被调用)。

See this for getting selected text from FlowDocument and see this for how you can set that text to clip board. 从越来越所选文本FlowDocument ,看到你如何设置文本到剪贴板。

Why not just right click and select "Paste Unformatted" (in Lync) or Past Keep Text Only (In Word). 为什么不右键单击并选择“粘贴未格式化”(在Lync中)或“过去仅保留文本”(在Word中)。 This will strip all formatting from the text in the clipboard 这将从剪贴板中的文本中删除所有格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM