RichTextBox的中文字着色,C# [英] Coloring Text in RichtextBox, C#
本文介绍了RichTextBox的中文字着色,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能彩色文本的新行与一些不同的颜色,然后将其添加到RichTextBox的?我使用SilverLight的
How can I color new line of text with some different colors and then add it to RichTextBox? I'm using SilverLight.
推荐答案
您可以在代码中做到这一点:
You can do this in code:
// Create a paragraph with two coloured runs
Paragraph para = new Paragraph();
Run run1 = new Run("Red ");
run1.Foreground = Brushes.Red;
Run run2 = new Run("Green");
run2.Foreground = Brushes.Green;
para.Inlines.Add(run1);
para.Inlines.Add(run2);
// Get the document
FlowDocument doc = richTextBox1.Document;
// Clear existing content
doc.Blocks.Clear();
// Add new content
doc.Blocks.Add(para);
或在XAML:
<RichTextBox Height="160" HorizontalAlignment="Left" Margin="43,20,0,0" Name="richTextBox1" VerticalAlignment="Top" Width="258" TextChanged="richTextBox1_TextChanged">
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Paragraph>
<Run Foreground="Red">Red</Run>
<Run Foreground="Green">Green</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
这篇关于RichTextBox的中文字着色,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文