25

Does anyone know how I can disable the text wrapping of a RichTextBox? E.g. if I have a large string which doesn't fit in the window, the RichTextBox places the part of the string which can't be shown of a new line. I want to disable that (and make it visible only by using the Scrollbar).

Thanks a lot.

Cheers

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Joseph jun. Melettukunnel
  • 6,267
  • 20
  • 69
  • 90

6 Answers6

41

A RichTextBox in WPF is simply an editor for a FlowDocument.
According to 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.

So, while there's no way for you to explicitly disable the word-wrapping of a RichTextBox, you can do something like this:

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

Which will have essentially the same desired effect until you have a line that exceeds the PageWidth.

Note (as of July 2015): VS2015 RC allows wordwrap = false to work precisely as OP seems to desire. I believe earlier versions of Visual Studio did also.

kiswa
  • 14,737
  • 1
  • 21
  • 29
Donut
  • 110,061
  • 20
  • 134
  • 146
  • 7
    I do not like the solution because then the horizontal Scroolbar is visible all the time... – Elisabeth Nov 02 '10 at 11:25
  • it would be nice if you could set that upon detecting (somehow) that wrapping is needed (e.g. check each "run" [paragraph] width or something) and turn it back off when not needed (monitoring text change to check everytime, although it might be slow) – George Birbilis Sep 07 '14 at 00:08
  • You can set `richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto` – Kris Vandermotten Jun 10 '15 at 15:32
  • `richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto` doesn't work – TWT Apr 24 '18 at 12:38
  • 1
    No, there is no WordWrap property for a WPF RichTextBox or FlowDocument control, unfortunately. – OfficeAddinDev Dec 05 '22 at 23:04
3

Since no answer was satisfying for me, here is my solution:

private void RichTxt_TextChanged(object sender, TextChangedEventArgs e)
{
    string text = new TextRange(richTxt.Document.ContentStart, richTxt.Document.ContentEnd).Text;
    FormattedText ft = new FormattedText(text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(richTxt.FontFamily, richTxt.FontStyle, richTxt.FontWeight, richTxt.FontStretch), richTxt.FontSize, Brushes.Black);
    richTxt.Document.PageWidth = ft.Width + 12;
    richTxt.HorizontalScrollBarVisibility = (richTxt.Width < ft.Width + 12) ? ScrollBarVisibility.Visible : ScrollBarVisibility.Hidden;
}

Question is about performance depending on text length and how often it is refreshed.

Martin
  • 93
  • 4
1

If you don't want to show the horizontal scrollbar, enforce a MinWidth on the ScrollViewer:

<RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Hidden">

    <RichTextBox.Resources>
        <Style TargetType="ScrollViewer">
            <Setter Property="MinWidth" Value="2000" />
        </Style>
    </RichTextBox.Resources>

</RichTextBox>
1

I also needed to display a large string and tried the RichTextBox but I did not like the solution with setting the PageWidth of the Document to a fixed size. The scrollbar would be visible all the time and the scrolling area was be to big.

If a TextBlock is sufficient you can use that instead and place it inside a ScrollViewer. It worked perfect for me since I did not need all the extra features of the RichTextBox.

<ScrollViewer Width="200"
              Height="100"
              HorizontalScrollBarVisibility="Auto"
              VerticalScrollBarVisibility="Auto">
                  <TextBlock TextWrapping="NoWrap">
                      <TextBlock.Text>
                          Very long text Very long text Very long text 
                      </TextBlock.Text>
                  </TextBlock>
</ScrollViewer>
TGasdf
  • 1,220
  • 12
  • 14
0

VerticalScrollBar :

VerticalScrollBarVisibility="Auto" MaxHeight="200"

HorizontalScrollBar :

HorizontalScrollBarVisibility="Auto" MaxWidth="400"

M Komaei
  • 7,006
  • 2
  • 28
  • 34
0

Suitable solution for me. The idea was taken from here. I defined in XAML

            <RichTextBox x:Name="PART_rtb" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Auto" TextChanged="MyRichTextBox_OnTextChanged">
                <RichTextBox.Document>
                    <FlowDocument x:Name="PART_fd"  >
                        <FlowDocument.Resources>
                            <!--This style is used to set the margins for all paragraphs in the FlowDocument to 0.-->
                            <Style TargetType="{x:Type Paragraph}">
                                <Setter Property="Margin" Value="3"/>

                            </Style>
                        </FlowDocument.Resources>
                    </FlowDocument>
                </RichTextBox.Document>
            </RichTextBox>

In Code

   private void MyRichTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        double i  = PART_rtb.Document.GetFormattedText().WidthIncludingTrailingWhitespace + 20;
        (sender as RichTextBox).Document.PageWidth = i;
    }