WPF RichTextBox 自动换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19589663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:57:16  来源:igfitidea点击:

WPF RichTextBox word wrapping

c#wpfrichtextboxword-wrap

提问by Stefan

I am trying to display a large amount of data in a WPF RichTextBox control. My data contains space characters. There is a default word wrapping behavior that does not allow a "word", to be split and displayed on more lines.

我正在尝试在 WPF RichTextBox 控件中显示大量数据。我的数据包含空格字符。有一个默认的自动换行行为,不允许将“单词”拆分并显示在更多行上。

This behavior is triggered by having space characters, questions marks, full stops or any other sentence/word delimiter. In the example below, if you replace the space character with a letter ( ex: "X" ), everything will be displayed as expected. As no delimiter characters are found, the big "word" is allowed to be truncated and displayed on multiple lines.

此行为由空格字符、问号、句号或任何其他句子/单词分隔符触发。在下面的示例中,如果您用字母(例如:“X”)替换空格字符,则所有内容都将按预期显示。由于没有找到分隔符,所以允许大“词”被截断并显示在多行上。

Is there a way of disabling this word/sentence wrapping behavior?

有没有办法禁用这个词/句子包装行为?

This is the XAML code:

这是 XAML 代码:

<Window x:Class="StackOverQuestion_TextBoxWrap.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="535">
    <Grid>
        <RichTextBox Name="RichTextBox" />
    </Grid>
</Window>

This is the cs code behind:

这是背后的cs代码:

 public MainWindow()
  {
     InitializeComponent();

     Random rnd = new Random();

     RichTextBox.FontFamily = new System.Windows.Media.FontFamily( "Lucida Console" );

     Paragraph par = new Paragraph();
     for ( int i = 0 ; i < 6000 ; i++ )
     {
        Run run = new Run();
        run.Text = rnd.NextDouble().ToString() + " " ;

        par.Inlines.Add( run );
     }
     RichTextBox.Document.Blocks.Add( par );
  }

Undesired wrapping behaviour: (please notice the different length of the lines)

不希望的包装行为:(请注意线的不同长度)

0.562230281327958 0.269015421750497 0.130114109315963 0.527640242375266 0.592048898149305
0.73868335026255 0.478530279117883 0.939313878276997 0.890535918479104 0.00047110533363703
0.546423877378192 0.780972927241108 0.697112546626997 0.66897076306351 0.634957212319112
0.498651245375467 0.808829494662969 

Desired wrapping behaviour: (please notice the same length of the lines)

所需的包装行为:(请注意线条的相同长度)

0.562230281327958 0.269015421750497 0.130114109315963 0.527640242375266 0.592048898149305
0.73868335026255 0.478530279117883 0.939313878276997 0.890535918479104 0.0004711053336370
3 0.546423877378192 0.780972927241108 0.697112546626997 0.66897076306351 0.63495721231911
2 0.498651245375467 0.808829494662969 

回答by Alberto Solano

I think you need to disable the word wrapping in the RichTextBox control, that is always enabled, according to the documentation in MSDN:

根据MSDN 中的文档,我认为您需要禁用 RichTextBox 控件中的自动换行,该控件始终处于启用状态:

Text always wraps in a RichTextBox. If you do not want text to wrap then set the PageWidthon the FlowDocumentto be larger than the width of the RichTextBox. However, once the page width is reached the text still wraps.

文本始终包含在 RichTextBox 中。如果您不希望文本换行,则将FlowDocument上的PageWidth设置为大于 RichTextBox 的宽度。但是,一旦达到页面宽度,文本仍然会换行。

There's no explicit property to disable it, and you can do something like this:

没有明确的属性可以禁用它,您可以执行以下操作:

richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
richTextBox1.Document.PageWidth = 1000;

as suggested here.

正如这里所建议的。