问题
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