Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 695135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:55:41+00:00 2026-05-14T02:55:41+00:00

I’m building an application that needs to allow a user to insert text from

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:55:42+00:00Added an answer on May 14, 2026 at 2:55 am

    Your immediate problem is that you are using TextFormat.Xaml instead of TextFormat.XamlPackage.

    The property that controls whether or not lines are merged when documents are combined is the Section.HasTrailingParagraphBreakOnPaste property. This property is only effective when loading or saving the XamlPackage text format. When using Xaml text format instead, the property is omitted during Save() and ignored during Load().

    So the simple fix is to simply change the Load and Save calls:

    tr.Save(ms, DataFormats.XamlPackage); 
    ms.Seek(0, SeekOrigin.Begin); 
    tr = new TextRange(target.CaretPosition, target.CaretPosition); 
    tr.Load(ms, DataFormats.XamlPackage); 
    

    Note that this also fixes another problem you would eventually run into: Embedded bitmaps will not be copied properly when using DataFormats.Xaml because there is nowhere to put the image bits. With DataFormats.XamlPackage an 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.ContentStart and document.ContentEnd. If this is your actual code you will discover that any range from document.ContentStart to document.ContentEnd necessarily 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 like RichTextBox.Selection (if this is UI driven) or use TextPointer to back up ContentEnd to before the implicit paragraph mark, for example:

    var tr = new TextRange(document.ContentStart,
                           document.ContentEnd.GetInsertionPosition(
                                                      LogicalDirection.Backward));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.