How to wrap text in RichTextBox?

我的梦境 提交于 2019-12-12 01:23:24

问题


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];

回答1:


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>



回答2:


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:

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;


来源:https://stackoverflow.com/questions/30999967/how-to-wrap-text-in-richtextbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!