WPF RichTextBox appending coloured text

前端 未结 4 1877
谎友^
谎友^ 2021-02-04 01:38

I\'m using the RichTextBox.AppendText function to add a string to my RichTextBox. I\'d like to set this with a particular colour. How can I do this?

4条回答
  •  眼角桃花
    2021-02-04 02:16

    Be Aware of TextRange's Overhead

    I spent a lot of time tearing my hair out, because TextRange wasn't fast enough for my use-case. This method avoids the overhead. I ran some barebones tests, and its faster by a factor of ~10 (but don't take my word for it lol, run your own tests)

    Paragraph paragraph = new Paragraph();
    Run run = new Run("MyText");
    paragraph.Inlines.Add(run);
    myRichTextBox.Document.Blocks.Add(paragraph);
    

    Credit

    Note: I think most use cases should work fine with TextRange. My use-case involved hundreds of individual appends, and that overhead stacks up.

提交回复
热议问题