Search code examples
wpfrichtextboxflowdocumentparagraph

Fastest way to update paragraph in RichTextBox's FlowDocument


I would update a paragraph in a FlowDocument (in a RichTextBox).

Currectly, I use, each time, the function Clear() to empty the blocks and, then, fill the RichTextBox.Document.Blocks with my paragraph.

But, clearing and adding each time my paragraph, it's very slow and I lost a lot of performance to do that. How optimize these instructions and update efficiently the paragraph in RichTextBox ?

Thank's


Solution

  • If I understand you correctly I suspect it is the run within the paragraph that is changing not the paragraph.

    If you are creating the paragraph/runs in code you may have not set the language.

    If you have a run without language set and you type in a RichTextBox the runs get split where you type to create 3 runs.

    Set the language on the Run/Paragraph to your locale e.g. en-gb or en-us

    var run = new Run("Test") {Language = XmlLanguage.GetLanguage("en-us") };
    

    If you now type in the middle of the run no new runs/paragraphs will be added.