I’m building an application that needs to allow a user to insert text from one RichTextBox at the current caret position in another one. I spent a lot of time screwing around with the FlowDocument‘s object model before running across this technique – source and target are both FlowDocuments:
using (MemoryStream ms = new MemoryStream())
{
TextRange tr = new TextRange(source.ContentStart, source.ContentEnd);
tr.Save(ms, DataFormats.Xaml);
ms.Seek(0, SeekOrigin.Begin);
tr = new TextRange(target.CaretPosition, target.CaretPosition);
tr.Load(ms, DataFormats.Xaml);
}
This works remarkably well.
The only problem I’m having with it now is that it always inserts the source as a new paragraph. It breaks the current run (or whatever) at the caret, inserts the source, and ends the paragraph. That’s appropriate if the source actually is a paragraph (or more than one paragraph), but not if it’s just (say) a line of text.
I think it’s likely that the answer to this is going to end up being checking the target to see if it consists entirely of a single block, and if it does, setting the TextRange to point at the beginning and end of the block’s content before saving it to the stream.
The entire world of the FlowDocument is a roiling sea of dark mysteries to me. I can become an expert at it if I have to (per Dostoevsky: “Man is the animal who can get used to anything.”), but if someone has already figured this out and can tell me how to do this it would make my life far easier.
Your immediate problem is that you are using
TextFormat.Xamlinstead ofTextFormat.XamlPackage.The property that controls whether or not lines are merged when documents are combined is the
Section.HasTrailingParagraphBreakOnPasteproperty. This property is only effective when loading or saving theXamlPackagetext format. When usingXamltext format instead, the property is omitted duringSave()and ignored duringLoad().So the simple fix is to simply change the Load and Save calls:
Note that this also fixes another problem you would eventually run into: Embedded bitmaps will not be copied properly when using
DataFormats.Xamlbecause there is nowhere to put the image bits. WithDataFormats.XamlPackagean entire package is built so bitmaps and other package items will come across ok.Once you make this change you may discover another fact that may or may not be an issue for you: Your sample code uses
document.ContentStartanddocument.ContentEnd. If this is your actual code you will discover that any range fromdocument.ContentStarttodocument.ContentEndnecessarily consists of full paragraphs, so copying it will always insert a paragraph break at the end of the insertion. If this is a problem, use something likeRichTextBox.Selection(if this is UI driven) or useTextPointerto back upContentEndto before the implicit paragraph mark, for example: