C#/WPF: Disable Text-Wrap of RichTextBox

前端 未结 6 1571
醉酒成梦
醉酒成梦 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:37

    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.

提交回复
热议问题