C# RichTextBlock在循环中添加新块

C# RichTextBlock在循环中添加新块,c#,uwp,richtextbox,windows-10-universal,C#,Uwp,Richtextbox,Windows 10 Universal,我想在RichTextBlock中添加一个新的普通Run,如果单词不匹配,并且如果匹配,则文本应为粗体: if (InnerTextofCell == "TEXT") { rtb2.Blocks.Add(new Paragraph (new Run { FontWeight = FontWeights.Bold, Text = innerTextOfCell })); } else { rtb2.Blocks.Add(new Paragraph (new Run { Text = i

我想在
RichTextBlock
中添加一个新的普通
Run
,如果单词不匹配,并且如果匹配,则文本应为粗体:

if (InnerTextofCell == "TEXT")
{
   rtb2.Blocks.Add(new Paragraph (new Run { FontWeight = FontWeights.Bold, Text = innerTextOfCell }));
}
else
{
   rtb2.Blocks.Add(new Paragraph (new Run { Text = innerTextOfCell }));
}
我唯一的问题是,
段落
没有包含1个参数的构造函数

有人有解决办法吗?
另外,它位于一个
foreach
循环中,因此它经常重复。

我认为您可以简单地这样做来解决您的问题:

if (InnerTextofCell == "TEXT")
{
   rtb2.Blocks.Add(new Paragraph (new Run { FontWeight = FontWeights.Bold, Text = innerTextOfCell }));
}
else
{
   rtb2.Blocks.Add(new Paragraph (new Run { FontWeight = FontWeights.Normal, Text = innerTextOfCell }));
}

我认为你可以简单地这样做来解决你的问题:

if (InnerTextofCell == "TEXT")
{
   rtb2.Blocks.Add(new Paragraph (new Run { FontWeight = FontWeights.Bold, Text = innerTextOfCell }));
}
else
{
   rtb2.Blocks.Add(new Paragraph (new Run { FontWeight = FontWeights.Normal, Text = innerTextOfCell }));
}

如果查看
段落
对象,您会注意到
内联线
属性(向其添加运行)是只读的。所以不能在构造函数中添加这些。一种可能的解决办法是:

var paragraph = new Paragraph();
var run = new Run { Text = innerTextOfCell };

if (InnerTextofCell == "TEXT")
{
    run.FontWeight = FontWeights.Bold;
}

paragraph.Inlines.Add(run);
rtb2.Blocks.Add(paragraph);
创建
段落
运行
对象,检查文本是否必须加粗,并将它们添加到
RichTextBlock

由于您谈论的是一个
foreach
循环,您甚至可以根据所追求的设计(文本在一行上或堆叠在多行上)重用
段落
对象。您的代码将类似于:

var paragraph = new Paragraph();

foreach(...)
{
    var run = new Run { Text = innerTextOfCell };

    if (InnerTextofCell == "TEXT")
    {
        run.FontWeight = FontWeights.Bold;
    }
}

paragraph.Inlines.Add(run);
rtb2.Blocks.Add(paragraph);

如果查看
段落
对象,您会注意到
内联线
属性(向其添加运行)是只读的。所以不能在构造函数中添加这些。一种可能的解决办法是:

var paragraph = new Paragraph();
var run = new Run { Text = innerTextOfCell };

if (InnerTextofCell == "TEXT")
{
    run.FontWeight = FontWeights.Bold;
}

paragraph.Inlines.Add(run);
rtb2.Blocks.Add(paragraph);
创建
段落
运行
对象,检查文本是否必须加粗,并将它们添加到
RichTextBlock

由于您谈论的是一个
foreach
循环,您甚至可以根据所追求的设计(文本在一行上或堆叠在多行上)重用
段落
对象。您的代码将类似于:

var paragraph = new Paragraph();

foreach(...)
{
    var run = new Run { Text = innerTextOfCell };

    if (InnerTextofCell == "TEXT")
    {
        run.FontWeight = FontWeights.Bold;
    }
}

paragraph.Inlines.Add(run);
rtb2.Blocks.Add(paragraph);
“段落不包含接受1个参数的构造函数”
,这是错误
段落
抛出的
“段落不包含接受1个参数的构造函数”
,这是错误
段落
抛出的