Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22104

Is it possible to assign value to a precreated Paragraph in RichTextBox

Consider the following XAML code:

<RichTextBox Name="dataRichTextBox" VerticalScrollBarVisibility="Auto" >
    <FlowDocument Name="dataFlowDocument" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Paragraph Name="dataParagraph">

        </Paragraph>
    </FlowDocument>
</RichTextBox>

What I would like to do is, directly assign a a Paragraph, created programmatically, to dataParagraph as defined in the XAML.

the code for that looks something like:

Paragraph paraOne = new Paragraph();
Run run1 = new Run("I am run one"+Environment.NewLine);
// run1.Background = Brushes.Green;
paraOne.Inlines.Add(run1);
dataParagraph = paraOne; // expect that it will show up on the RichTextBox.

I have tried it, and it doesn't work. The examples I read so far, all seem to create the FlowDocument, Paragraph Programmatically and then assign Runs' to them. Is it possible to achieve it the way I have implemented.

Upvotes: 1

Views: 512

Answers (1)

Jay
Jay

Reputation: 57939

You can't replace the paragraph declared in XAML with a new one, but you can work with it directly.

Keeping your XAML as-is, change your code to this, and it will work:

dataParagraph.Inlines.Add(new Run("I am run one" + Environment.NewLine));

Alternatively, just add the new paragraph to the FlowDocument, rather than trying to assign it to the existing paragraph.

Upvotes: 1

Related Questions