2019-11-7-WPF-How-to-get-plain-text-from-RichTextBox

title author date CreateTime categories
WPF How to get plain text from RichTextBox
lindexi
2019-11-07 10:26:17 +0800
2019-11-07 10:26:16 +0800
WPF

We can not find any function to get plain text from RichTextBox. But we can use TextRange to get plain text.

We create a RichTextBox in UI

<RichTextBox Name="RichTextBox">
  <FlowDocument>
    <Paragraph>
      <Run>Paragraph 1</Run>
    </Paragraph>
    <Paragraph>
      <Run>Paragraph 2</Run>
    </Paragraph>
    <Paragraph>
      <Run>Paragraph 3</Run>
    </Paragraph>
  </FlowDocument>
</RichTextBox>

And we can use TextRange to get plain text from RichTextBox

 string text = new TextRange(RichTextBox.Document.ContentStart, RichTextBox.Document.ContentEnd).Text

See: How to: Extract the Text Content from a RichTextBox

posted @ 2019-11-21 16:09  lindexi  阅读(60)  评论(0编辑  收藏  举报