Stef
Stef

Reputation: 149

C# String in RichTextBox Wpf

I have the following question:

I have a class Question which has a property BodyString of the type String. How do I put this String in a Wpf-RichTextBox?

Upvotes: 3

Views: 8681

Answers (2)

Michal Ciechan
Michal Ciechan

Reputation: 13888

From this answer:

The WPF RichTextBox has a Document property for setting the content a la MSDN:

// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();

// Add paragraphs to the FlowDocument.
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
RichTextBox myRichTextBox = new RichTextBox();

// Add initial content to the RichTextBox.
myRichTextBox.Document = myFlowDoc;

You can just use the AppendText method though if that's all you're after.

Hope that helps.

Upvotes: 1

oopbase
oopbase

Reputation: 11385

yourRichTextBox.AppendText(BodyString);

Upvotes: 6

Related Questions