I need to get exactly all text from a WPF RichTextBox.
Everyone (How to get a WPF rich textbox into a string, RichTextBox (WPF) does not have string property "Text", http://msdn.microsoft.com/en-us/library/ms754041(v=vs.110).aspx and many more) agrees a code like this one:
public static string GetText(RichTextBox rtb)
{
TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
string text = textRange.Text;
return text;
}
but it add a trailling "\r\n". In a very simple case:
<RichTextBox x:Name="mRichTextBox">
<FlowDocument>
<Section>
<Paragraph>
<Run Text="The dog is brown. The cat is black."/>
</Paragraph>
</Section>
</FlowDocument>
</RichTextBox>
I get:
"The dog is brown. The cat is black.\r\n"
It can be seen as a minor issue but really I need exact text.
- Do you know why?
- Is it safe to simply ignore one trailing "\r\n"?
- Is my code correct?
- Is there any other gotcha (I mean differences between real text and what I get from my GetText)?
Thanks.