C#/WPF:禁用 RichTextBox 的文本换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1368047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:44:33  来源:igfitidea点击:

C#/WPF: Disable Text-Wrap of RichTextBox

c#wpfrichtextboxword-wrap

提问by Joseph jun. Melettukunnel

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 RichTextBoxplaces 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).

有谁知道如何禁用 a 的文本换行RichTextBox?例如,如果我有一个不适合窗口的大字符串,则将RichTextBox无法显示的字符串部分放置在新行中。我想禁用它(并仅通过使用 使其可见Scrollbar)。

Thanks a lot.

非常感谢。

Cheers

干杯

采纳答案by Donut

A RichTextBoxin WPF is simply an editor for a FlowDocument.
According to MSDN:

RichTextBoxWPF 中的A只是一个FlowDocument.
根据MSDN

Text always wraps in a RichTextBox. If you do not want text to wrap then set the PageWidthon the FlowDocumentto be larger than the width of the RichTextBox. However, once the page width is reached the text still wraps.

文本始终包含在RichTextBox 中。如果您不希望文本换行,则将FlowDocument上的PageWidth设置为大于RichTextBox的宽度 。但是,一旦达到页面宽度,文本仍然会换行。

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

因此,虽然您无法明确禁用 a 的自动换行RichTextBox,但您可以执行以下操作:

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.

这将具有基本相同的预期效果,直到您有一条超过PageWidth.

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

注意(截至 2015 年 7 月):VS2015 RC 允许wordwrap = false按照 OP 的要求精确工作。我相信早期版本的 Visual Studio 也是如此。

回答by TGasdf

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.

我还需要显示一个大字符串并尝试了 RichTextBox,但我不喜欢将文档的 PageWidth 设置为固定大小的解决方案。滚动条一直可见,滚动区域很大。

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.

如果 TextBlock 就足够了,您可以改用它并将其放置在 ScrollViewer 中。它非常适合我,因为我不需要 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>

回答by Peterson Salamat

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

如果您不想显示水平滚动条,请在 ScrollViewer 上强制使用 MinWidth:

<RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Hidden">

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

</RichTextBox>

回答by Martin

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.

问题是关于取决于文本长度和刷新频率的性能。

回答by M Komaei

VerticalScrollBar :

垂直滚动条:

VerticalScrollBarVisibility="Auto" MaxHeight="200"

VerticalScrollBarVisibility="Auto" MaxHeight="200"

HorizontalScrollBar :

水平滚动条:

HorizontalScrollBarVisibility="Auto" MaxWidth="400"

Horizo​​ntalScrollBarVisibility="Auto" MaxWidth="400"