开发者

Coloring Text in RichtextBox, C#

开发者 https://www.devze.com 2023-01-16 21:02 出处:网络
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:

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

Or in 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消