【问题标题】:Coloring Text in RichtextBox, C#在 RichtextBox 中为文本着色,C#
【发布时间】:2011-04-15 07:09:33
【问题描述】:

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

【问题讨论】:

    标签: c# silverlight richtextbox


    【解决方案1】:

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

    // 创建一个有两个彩色运行的段落 段落 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多