简体   繁体   English

WPF RichTextBox文字环绕

[英]WPF RichTextBox word wrapping

I am trying to display a large amount of data in a WPF RichTextBox control. 我试图在WPF RichTextBox控件中显示大量数据。 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. 有一个默认的自动换行行为,不允许拆分“单词”并在更多行上显示。

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. 在下面的示例中,如果将空格字符替换为字母(例如:“ X”),则所有内容将按预期显示。 As no delimiter characters are found, the big "word" is allowed to be truncated and displayed on multiple lines. 由于找不到分隔符,因此大的“单词”被截断并显示在多行上。

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 

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. 文本总是包装在RichTextBox中。 If you do not want text to wrap then set the PageWidth on the FlowDocument to be larger than the width of the RichTextBox. 如果您不希望自动换行,则将FlowDocumentPageWidth设置为大于RichTextBox的宽度。 However, once the page width is reached the text still wraps. 但是,一旦达到页面宽度,文本仍会自动换行。

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 . 这里建议的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM