How to change highlight colour of a portion of a text in rich text box?

送分小仙女□ 提交于 2019-12-11 09:51:55

问题


I am new to WPF's RichTextBox. I would like to know how to highlight text on a line with a specific colour.

Let's say I have a rich text box with a yellow background and assign to it a flow document.

richTextBox.Background = Brushes.LightYellow;
var mcFlowDoc = new FlowDocument();
var para = new Paragraph();
para.Inlines.Add(new Run("This is the first line.\n"));
para.Inlines.Add(new Run("This is the second line.\n"));
para.Inlines.Add(new Run("This is the third line."));
mcFlowDoc.Blocks.Add(para);
richTextBox.Document = mcFlowDoc;

What would I have to do next to change the third line's highlight colour to red? I am not talking about selection highlight colour, but normal text highligting (like in WordPad)

If there is a solution, I would like it in C# code, I want to stay away from XAML editing.


回答1:


    Run run = new Run("Red is the third line.\n");
    // run.Foreground = Brushes.Red;
    run.Background = Brushes.Red;
    para.Inlines.Add(run);


来源:https://stackoverflow.com/questions/12435340/how-to-change-highlight-colour-of-a-portion-of-a-text-in-rich-text-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!