如何在富文本框中更改部分文本的突出显示颜色? [英] How to change highlight colour of a portion of a text in rich text box?

查看:645
本文介绍了如何在富文本框中更改部分文本的突出显示颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的RichTextBox的新手.我想知道如何在具有特定颜色的行上突出显示文本.

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)

如果有解决方案,我希望使用C#代码,但我希望远离XAML编辑.

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

推荐答案

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

这篇关于如何在富文本框中更改部分文本的突出显示颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆