Color different parts of a RichTextBox string

前端 未结 9 818
暖寄归人
暖寄归人 2020-11-22 08:10

I\'m trying to color parts of a string to be appended to a RichTextBox. I have a string built from different strings.

string temp = \"[\" + DateTime.Now.ToSh         


        
9条回答
  •  借酒劲吻你
    2020-11-22 08:36

    I think modifying a "selected text" in a RichTextBox isn't the right way to add colored text. So here a method to add a "color block" :

            Run run = new Run("This is my text");
            run.Foreground = new SolidColorBrush(Colors.Red); // My Color
            Paragraph paragraph = new Paragraph(run);
            MyRichTextBlock.Document.Blocks.Add(paragraph);
    

    From MSDN :

    The Blocks property is the content property of RichTextBox. It is a collection of Paragraph elements. Content in each Paragraph element can contain the following elements:

    • Inline

    • InlineUIContainer

    • Run

    • Span

    • Bold

    • Hyperlink

    • Italic

    • Underline

    • LineBreak

    So I think you have to split your string depending on parts color, and create as many Run objects as needed.

提交回复
热议问题