如何在富文本框中更改部分文本的突出显示颜色?

编程入门 行业动态 更新时间:2024-10-26 09:33:33
本文介绍了如何在富文本框中更改部分文本的突出显示颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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);

更多推荐

如何在富文本框中更改部分文本的突出显示颜色?

本文发布于:2023-06-09 04:10:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/594954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   框中   颜色   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!