Change color and font for some part of text in WPF C#

后端 未结 4 1108
孤城傲影
孤城傲影 2020-11-27 18:25

Is there a way to change color and font for some part of text which I want to put on TextBox or RichTextBox. I am using C# WPF.

For example

 richText         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 18:59

    You can try out this.

    public TestWindow()
    {
        InitializeComponent();
    
        this.paragraph = new Paragraph();
        rich1.Document = new FlowDocument(paragraph);
    
        var from = "user1";
        var text = "chat message goes here";
        paragraph.Inlines.Add(new Bold(new Run(from + ": "))
        {
            Foreground = Brushes.Red
        });
        paragraph.Inlines.Add(text);
        paragraph.Inlines.Add(new LineBreak());
        this.DataContext = this;
    }
    private Paragraph paragraph;
    

    So use the Document property of the RichTextBox

提交回复
热议问题