C#/WPF: Disable Text-Wrap of RichTextBox
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>
Related videos on Youtube
TastyEquation60
Head of Product at divio.com, the company behind Aldryn and backers of django-CMS.
Updated on July 09, 2022Comments
-
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, theRichTextBox
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 theScrollbar
).Thanks a lot.
Cheers
-
Elisabeth over 13 yearsI do not like the solution because then the horizontal Scroolbar is visible all the time...
-
George Birbilis over 9 yearsa TextBlock isn't editable, plus I don't think it supports rich formatting
-
George Birbilis over 9 yearsit 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 over 9 years@GeorgeBirbilis TextBlock is not editable but it support rich formatting using nested formatting tags. Bold, Hyperlink, etc.
-
Kris Vandermotten almost 9 yearsYou can set
richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
-
TWT about 6 years
richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
doesn't work