如何在 RichTextBox 中换行? [英] How to wrap text in RichTextBox?

查看:89
本文介绍了如何在 RichTextBox 中换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

富文本框是固定宽度的.而且我不想显示水平滚动.我想用正确的文字换行来修复 Richtextbox 中的内容.

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.

我有一个包含 3 行的富文本框.文本换行设置为Wrap"但文本被包裹如下:

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

但我想如何包装它:

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.

已这是 XAML 代码:

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)

添加代码和说明:

主窗口中有很多富文本框.我将他们所有的流程文档保存在一个数组中.(主窗口:阵列名称:fdsongs)

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

现在以全屏显示的另一种形式,我一次显示一个富文本框并从数组中分配流文档.(全屏:数组 fdsongs1 是主窗口中 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];

推荐答案

试试这个

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

这篇关于如何在 RichTextBox 中换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆