C#/WPF: Disable Text-Wrap of RichTextBox

前端 未结 6 1557
醉酒成梦
醉酒成梦 2020-12-01 20:44

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

6条回答
  •  情话喂你
    2020-12-01 21:22

    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.

提交回复
热议问题