0

Recently I encountered a problem with my application: when I copy paste the text out of my FlowDocument, it saves the color! This is an issue because other applications (like Microsoft Lync and Outlook) accept this color when pasting! 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. Copy pasting out of a TextBox does not preserve color.

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?

P.S. Please do not post answers telling me to use a TextBox instead of a FlowDocument. There's a reason I'm using those classes in their respective places, and I cannot interchange them.

2
  • you want copy pasting in your flow document or somewhere else? Jul 15, 2014 at 3:32
  • if you want flowdocument to get pasted in richtextbox then you can get OnPaste Event of RichTextbox.. Jul 15, 2014 at 3:34

3 Answers 3

3

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:

<FlowDocumentScrollViewer Name="flowDocumentViewer">
    <FlowDocument>
1
  • Works right out of the box. Thanks for the code samples!
    – Darkhydro
    Jul 15, 2014 at 16:46
1

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. 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 e.g. Ctrl+Shift+c, so that it won't conflict with the default copy functionality.

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).

See this for getting selected text from FlowDocument and see this for how you can set that text to clip board.

1
  • I want to override the default copy so that the user has the same experience in my application as all other windows applications. I definitely don't want a new keyboard sequence being required. Thanks for the answer though.
    – Darkhydro
    Jul 15, 2014 at 16:45
0

Why not just right click and select "Paste Unformatted" (in Lync) or Past Keep Text Only (In Word). This will strip all formatting from the text in the clipboard

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.