1
<RichTextBox AcceptsTab="True" ForceCursor="True" IsDocumentEnabled="True" TextChanged="ContentChanged" Name="TextContent"/>

In C# file i am not able to get Text property of Rich Textbox. I am trying to get this like;

TextContent.Text= "hello"

But it is giving compile time error.

'System.Windows.Controls.RichTextBox' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Windows.Controls.RichTextBox' could be found (are you missing a using directive or an assembly reference?) 

Please suggest me.

kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
Durga Dutt
  • 4,093
  • 11
  • 33
  • 48
  • 1
    You need to use the Document property, have a look here: http://stackoverflow.com/questions/957441/richtextbox-wpf-does-not-have-string-property-text – Adrian Fâciu Mar 14 '11 at 06:43
  • are you actually trying to read or write content? Because you asking how to get and trying to set in example... Anyway, i have added examples in my answer below. – kyrylomyr Mar 14 '11 at 17:38

2 Answers2

5

Generally, you need to work with Blocks property. But, if you are using FlowDocument for representing RichTextBox content, then you can access text with Document property.

For example, writing content:

XAML:

<RichTextBox Name="rtb">
</RichTextBox>

Code:

FlowDocument contentForStoring =
    new FlowDocument(new Paragraph(new Run("Hello, Stack Overflow!")));
rtb.Document = contentForStoring;

To read content you simply access Document property:

FlowDocument yourStoredContent = rtb.Document;

If you need just to take text, you have more simple way - TextRange class. Next code will retrieve all text content:

TextRange storedTextContent =
    new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
string yourText = storedTextContent.Text;
kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
0

If you want to retrieve the text from the rich text box then use this code,

string content = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;
Devendra D. Chavan
  • 8,871
  • 4
  • 31
  • 35