2

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.

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.

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

This is the XAML code:

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

 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 
Stefan
  • 21
  • 1
  • 4
  • 1
    Possible duplicate of [C#/WPF: Disable Text-Wrap of RichTextBox](http://stackoverflow.com/questions/1368047/c-wpf-disable-text-wrap-of-richtextbox) – NoWar May 09 '16 at 18:44

1 Answers1

4

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

Text always wraps in a 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. 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.

Community
  • 1
  • 1
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
  • Hi Alberto. Thank you for the response. I have already tried this approach, but I don't want the content to be displayed on only one line. I need it to wrap on multiple lines, without trying to prevent words from splitting. – Stefan Oct 25 '13 at 12:50
  • @Stefan You're welcome. One line? The PageWidth fixes only the width of the page in which the contents are displayed. Set for example PageWidth = 500, your contents won't be displayed on only one line (tried with Kaxaml). – Alberto Solano Oct 25 '13 at 13:49
  • Thanks again. I have just tried to set the PageWidth to 500. For this particular data pattern, it seems to work as all the data would fit in 2 columns. It does not work if the data pattern is different and we have more space characters. Also, this solution does not work when trying to resize the window. I would expect the contents to auto-resize, without having a fixed size. I don't want to get rid of the wrapping behavior completely, I just need a way of making it not depend on sentence/word separators (space, full stop, question mark, etc characters), as shown in the original question. – Stefan Oct 25 '13 at 14:03