【问题标题】:WPF: How do I set the content of a Paragraph in Code?WPF:如何在代码中设置段落的内容?
【发布时间】:2010-09-23 16:48:47
【问题描述】:

我有一个 FlowDocument 并为一个段落分配了一个名称。

我想编辑一个段落的内容(顺便说一句,这只是一个普通的字符串)。

如何做到这一点?

【问题讨论】:

    标签: c# .net wpf visual-studio xaml


    【解决方案1】:
    var paragraph = new Paragraph();
    paragraph.Inlines.Add(new Run(yourString));
    flowDocument.Blocks.Add(paragraph);
    

    【讨论】:

    • 好的,如何更改内联的内容(字符串)?
    • flowDocument.Blocks 是一个可枚举的集合,因此您可以使用例如 foreach 语句对其进行迭代。
    • 非常感谢。就是这样。不介意的也请看这个问题:stackoverflow.com/questions/3781109/…
    【解决方案2】:

    可以通过这种方式更改现有段落的文本。 (段落的个别格式会丢失!)

    // myParagraph is the paragraph with the old text
    
    while (myParagraph.Inlines.Count > 0)
        myParagraph.Inlines.Remove(myParagraph.Inlines.ElementAt(0));
    
    myParagraph.Inlines.Add(new Run("new text"));
    

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 2014-01-27
      • 1970-01-01
      • 2011-06-04
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      相关资源
      最近更新 更多