【问题标题】:How can I get the whole text content from a Paragraph(System.Windows.Documents)?如何从段落(System.Windows.Documents)中获取整个文本内容?
【发布时间】:2015-07-21 18:40:00
【问题描述】:

我认为问题标题足以自我解释。 我通过ParagraphControl.Inlines.Add(SomeStringValue) 添加了一些字符串,但在检索文本内容时遇到了一些麻烦。有什么建议吗?

我尝试从Inline 本身获取文本,然后通过String.Join() 展平字符串集合

【问题讨论】:

    标签: c# wpf string paragraph inlines


    【解决方案1】:

    我终于成功了。

    Text = String.Join(String.Empty, ParagraphControl.Inlines.Select(line => line.ContentStart.GetTextInRun(LogicalDirection.Forward)));
    

    【讨论】:

      【解决方案2】:

      这是我使用的一种扩展方法。 ParagraphTextElement,System.Windows.Document 中的其他一些东西也是如此。

      public static string GetText(this TextElement textElement)
      {
          return new TextRange(textElement.ContentStart, textElement.ContentEnd).Text;
      }
      

      另见https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-extract-the-text-content-from-a-richtextbox

      【讨论】:

        猜你喜欢
        • 2014-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-19
        • 2011-01-24
        • 2010-11-28
        • 1970-01-01
        • 2012-07-22
        相关资源
        最近更新 更多