How to wrap text in RichTextBox?

4.4k Views Asked by At

The richtext box is of fixed width. And I don't want to show a horizontal scroll. I want to fix the content in the richtextbox with proper wrapping of words.

I have a richtext box with 3 lines. The textwrapping is set to "Wrap" But the text is wrapped like below:

Amazing grace how sweet the sou

nd

That saved a wretch lik

e me

I once was lost but now found

But how I wanted to wrap it is:

Amazing grace how sweet the 

sound

That saved a wretch

like me

I once was lost but now found

How do I achieve this? I need it to wrap the text on multiple lines, without trying to prevent words from splitting.

Edited: This is the XAML code:

 <RichTextBox FontSize="60" IsReadOnly="True" x:Name="rtbText" BorderThickness="0" Background="Transparent" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,0,0" />

The flowdocument to the richtextbox is assigned during the run time (dynamically)

Adding code and explanation:

There are many richtext boxes in the main window. I save all their flow documents in an array. (main window: array name: fdsongs)

Now in another form, which is shown in Fullscreen, I show one richtext box at a time and assign the flow documents from the array. ( fullscreen: array fdsongs1 is a copy of fdsongs from main window)

Before assigning the flow document in the fullscreen richtext box, I alter the font size and the text alignment of the blocks and then assign the flow document to the richtextbox.

Block[] b1 = fdsongs1[0].Blocks.ToArray();

 foreach (Block b in b1)
 {
       b.TextAlignment = TextAlignment.Center;
b.FontSize = myCalcFontSize;

 }

 rtbText.Document = fdsongs1[0];
3

There are 3 best solutions below

0
On

Try this

   <RichTextBox>
        <RichTextBox.Document>
            <FlowDocument>
                <Paragraph>
                    <TextBlock Foreground="Red" TextWrapping="Wrap" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."></TextBlock>
                </Paragraph>
            </FlowDocument>
        </RichTextBox.Document>
    </RichTextBox>
13
On

Your RichTextBox should contain a FlowDocument and the text will wrap as you want it to.

Look at this sample:

<Window x:Class="WpfApplication9.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel Margin="0,0,331,0">
        <RichTextBox AcceptsReturn="True">
            <FlowDocument>
                <Paragraph>Amazing grace how sweet the sound</Paragraph>
            </FlowDocument>
        </RichTextBox>
        <RichTextBox AcceptsReturn="True">
            <FlowDocument>
                <Paragraph>That saved a wretch like me</Paragraph>
            </FlowDocument>
        </RichTextBox>
        <RichTextBox AcceptsReturn="True">
            <FlowDocument>
                <Paragraph>I once was lost but now found</Paragraph>
            </FlowDocument>
        </RichTextBox>
    </StackPanel>
</Grid>

It gives me exactly what you want:

enter image description here

EDIT: Here is how to do it programatically.

            TextBlock tb = new TextBlock();
            tb.TextWrapping = TextWrapping.Wrap;
            tb.Text = "This is a very long not so long text with multiple words";

            Paragraph p = new Paragraph();
            p.Inlines.Add(tb);

            FlowDocument fd = new FlowDocument();
            fd.Blocks.Add(p);

            rtbTest.Document = fd;
0
On

If you want wrapped text in your Wpf RichTextBox you can use the Document.PageWidth property of your RichTextBox. Set the size to the actual width of your RichTextBox control and your text lines are wrapped. I personally do not set the value by design time and instead subscribe to the SizeChanged event to ensure to get the right width at any time.

MyRichTextBox.SizeChanged += (s, e) => MyRichTextBox.Document.PageWidth = MyRichTextBox.ActualWidth;

If you have enabled the vertical scrollbar you may want to subtract the width of the scrollbar, so all text is always visible in the RichTextBox:

MyRichTextBox.SizeChanged += (s, e) => MyRichTextBox.Document.PageWidth = MyRichTextBox.ActualWidth - 30;