wpf RichTextBox的使用(总结)

今天就想在Textbox上显示Error的信息的时候,字体变红色,发现并不支持。所以研究了下RichTextBox。留着以后备用。

(1) 设置行间距:

           
               
                   
               

           

 

(2)清空:

if (txtMessage.Document.Blocks.Count() > 200)

{

txtMessage.Document.Blocks.Clear();

}

(3)添加文本:

方式1: richTextBox.AppendText("p: "+ p);

             richTextBox.AppendText("\n");

方式2:txtMessage.Document.Blocks.Add(new Paragraph(new Run(pMessage) { Foreground = Brushes.Black }));//可以设置字体颜色,字体粗细等。

例:

void appendTextWithColor(string pText,Brush pColor,FontWeight pFontWeight)

{

richTextBox.Document.Blocks.Add(new Paragraph(new Run(pText) { Foreground = pColor, FontWeight = pFontWeight}));

}

 

调用:

appendTextWithColor("hello",Brushes.Red,FontWeights.Bold);

appendTextWithColor("me too", Brushes.Green,FontWeights.Normal);

 

你可能感兴趣的:(wpf RichTextBox的使用(总结))