I am having trouble removing the formatting (bold, font, size etc.) on paste of some formatted text into a RichTextBox.
I have successfully been using the following:
- Add a pasting handler to the RichTextBox
DataObject.AddPastingHandler(RichTextBoxControl, TextBoxPasting);
- Remove formatting and insert text
private void TextBoxPasting(object sender, DataObjectPastingEventArgs e)
{
var pastingText = e.DataObject.GetData(DataFormats.Text) as string;
RichTextBoxControl.Document.ContentEnd.InsertTextInRun(pastingText);
e.CancelCommand();
}
But unfortunately this always places the inserted text in the end of the RichTextBox. Also the caret is not moving.
Let us say you are at this positing:
Helloꕯ World
and you are pasting Beautiful
you would get Helloꕯ World Beutiful
instead of Hello Beutifulꕯ World
.