C#/WPF: Disable Text-Wrap of RichTextBox

30,304

Solution 1

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.

Solution 2

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.

Solution 3

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>
Share:
30,304

Related videos on Youtube

TastyEquation60
Author by

TastyEquation60

Head of Product at divio.com, the company behind Aldryn and backers of django-CMS.

Updated on July 09, 2022

Comments

  • TastyEquation60
    TastyEquation60 almost 2 years

    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

  • Elisabeth
    Elisabeth over 13 years
    I do not like the solution because then the horizontal Scroolbar is visible all the time...
  • George Birbilis
    George Birbilis over 9 years
    a TextBlock isn't editable, plus I don't think it supports rich formatting
  • George Birbilis
    George Birbilis over 9 years
    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)
  • SepehrM
    SepehrM over 9 years
    @GeorgeBirbilis TextBlock is not editable but it support rich formatting using nested formatting tags. Bold, Hyperlink, etc.
  • Kris Vandermotten
    Kris Vandermotten almost 9 years
    You can set richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
  • TWT
    TWT about 6 years
    richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto doesn't work