How to create a RichTextBox in WPF

The following code creates a RichTextBox in WPF and adds a flow document to it.

The RichTextBox element in XAML represents WPF RichTextBox control.


<Window x:Class="RichTextBoxSample.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300">

    <Grid>

        <RichTextBox Name="RTB" Height="167" Margin="0,0,0,95" Width="278">

            <FlowDocument>

                <Paragraph>

                    <Run>First paragraph goes here. Type some text here.</Run>

                </Paragraph>

                <Paragraph>

                    <Run>Second paragraph goes here. Type some text here.</Run>

                </Paragraph>         

            </FlowDocument>

        </RichTextBox>

        

    </Grid>

</Window>