Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: How do I set the content of a Paragraph in Code?

I've got a FlowDocument and assigned a name to one paragraph.

I want to edit the content of a paragraph (which is just one ordinary string btw.).

How to do this?

like image 407
Hedge Avatar asked Sep 23 '10 16:09

Hedge


People also ask

Can I embed a figure into a paragraph of text in WPF?

Figure and Floater are used to embed content in Flow Documents with placement properties that can be customized independent of the primary content flow.

What is Flowdocument in WPF?

Flow documents are designed to optimize viewing and readability. Rather than being set to one predefined layout, flow documents dynamically adjust and reflow their content based on run-time variables such as window size, device resolution, and optional user preferences.

How do I center text in WPF?

If you want to center each line, use a TextBlock instead, and set TextAlignment="Center" .


2 Answers

To change the text of an existing paragraph can be done this way. (Individual formattings of the paragraph get lost!)

// myParagraph is the paragraph with the old text

while (myParagraph.Inlines.Count > 0)
    myParagraph.Inlines.Remove(myParagraph.Inlines.ElementAt(0));

myParagraph.Inlines.Add(new Run("new text"));
like image 175
marsh-wiggle Avatar answered Oct 04 '22 02:10

marsh-wiggle


var paragraph = new Paragraph();
paragraph.Inlines.Add(new Run(yourString));
flowDocument.Blocks.Add(paragraph);
like image 36
Kent Boogaart Avatar answered Oct 04 '22 02:10

Kent Boogaart