Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在richtextbox中添加不同格式的段落_C#_Wpf_Formatting_Richtextbox_Paragraph - Fatal编程技术网

C# 如何在richtextbox中添加不同格式的段落

C# 如何在richtextbox中添加不同格式的段落,c#,wpf,formatting,richtextbox,paragraph,C#,Wpf,Formatting,Richtextbox,Paragraph,我有一个richtextbox <RichTextBox x:Name="rtbReport_RTF" Margin="10" BorderBrush="Gray" Background="White" Padding="0"/> 我需要做的是在同一段中有不同的格式。就像 你好,世界 与你好正常和世界!背景为黄色,前景为红色 谢谢您可以使用流程文档 FlowDocument doc = new FlowDocument(); Paragraph par = new Paragr

我有一个richtextbox

<RichTextBox x:Name="rtbReport_RTF" Margin="10"  BorderBrush="Gray" Background="White" Padding="0"/>
我需要做的是在同一段中有不同的格式。就像

你好,世界

与你好正常和世界!背景为黄色,前景为红色


谢谢

您可以使用流程文档

FlowDocument doc = new FlowDocument();

Paragraph par = new Paragraph();

Run run1 = new Run("Hello");
par.Inlines.Add(run1);

Run run2 = new Run("World");
run2.Background = Brushes.Yellow;
run2.Foreground = Brushes.Red;
run2.FontWeight = FontWeights.Bold;
par.Inlines.Add(run2);

doc.Blocks.Add(par);

myRichTextBox.Document = doc;

您可以使用flowdocument

FlowDocument doc = new FlowDocument();

Paragraph par = new Paragraph();

Run run1 = new Run("Hello");
par.Inlines.Add(run1);

Run run2 = new Run("World");
run2.Background = Brushes.Yellow;
run2.Foreground = Brushes.Red;
run2.FontWeight = FontWeights.Bold;
par.Inlines.Add(run2);

doc.Blocks.Add(par);

myRichTextBox.Document = doc;
看看,看看。