RichtextBox中的着色文本,C#

Smi*_*gey 6 c# silverlight richtextbox

如何使用一些不同的颜色为新的文本行着色,然后将其添加到RichTextBox?我正在使用SilverLight.

arx*_*arx 2

您可以在代码中执行此操作:

    // 创建一个带有两个彩色运行的段落
    段落 para = new Paragraph();
    运行 run1 = new Run("红色");
    run1.Foreground = Brushes.Red;
    运行 run2 = new Run("绿色");
    run2.Foreground = Brushes.Green;
    Para.Inlines.Add(run1);
    Para.Inlines.Add(run2);
    // 获取文档
    FlowDocument doc = richTextBox1.Document;
    // 清除现有内容
    doc.Blocks.Clear();
    // 添加新内容
    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>
Run Code Online (Sandbox Code Playgroud)