如何清除 Paragraph.Inline 中的所有属性

How to clear all properties from Paragraph.Inline

是否可以从 Paragraph.Inline 元素中清除所有属性,例如(背景颜色...),就像您可以使用 class TextRange 一样?


好吧,我想清除内联集合中先前 运行 元素的背景 属性。所以我认为调用一个清除所有先前属性的方法会更容易。 但是,就我而言,似乎唯一的方法是这样的:

int index = 0;
...
List<Inline> runList = ParagraphComponent.Inlines.ToList(); 
if (index < runList.Count) {
    if (index > 1) {
       int previousPartIndex = index - 2;
       if (!string.IsNullOrEmpty(runList[previousPartIndex].Text)) {
          runList[previousPartIndex].Background = null;
       }
    }
    runList[index].Background = BackgroundColor;
    index += 2;
}

由于您无法通过索引访问 InlineCollection,我建议使用原始的 _inlineCollection 来初始化段落的内联(从您的 )。

((Run)_inlineCollection[index]).Background = null;
index++;
while (index < inlineCollection.Count && !(_inlineCollection[index] is Run))
{
    index++;
}
if (index < _inlineCollection.Count)
{
    ((Run)_inlineCollection[index]).Background = BackgroundColor;
}