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

发布于 2024-08-03 05:03:03 字数 157 浏览 5 评论 0原文

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

多谢。

干杯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

橙幽之幻 2024-08-10 05:03:03

RichTextBox在 WPF 中,它只是 FlowDocument< 的编辑器/代码>
根据 MSDN

文本始终换行在 RichTextBox 中。如果
你不希望文本换行然后设置
页面宽度 ="http://msdn.microsoft.com/en-us/library/system.windows.documents.flowdocument.aspx" rel="noreferrer">FlowDocument 到
大于宽度
RichTextBox。然而,一旦页面
达到宽度后文本仍然换行。

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

richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
richTextBox1.Document.PageWidth = 1000;

这将具有基本上相同的预期效果,直到您有一行超出 <代码>页面宽度。

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

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.

听闻余生 2024-08-10 05:03:03

由于没有答案让我满意,这是我的解决方案:

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;
}

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

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.

鹿! 2024-08-10 05:03:03

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

如果 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>

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.

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.

<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>
书信已泛黄 2024-08-10 05:03:03

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

<RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Hidden">

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

</RichTextBox>

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>
℡寂寞咖啡 2024-08-10 05:03:03

VerticalScrollBar:

VerticalScrollBarVisibility="自动" MaxHeight="200"

Horizo​​ntalScrollBar:

Horizo​​ntalScrollBarVisibility="自动" MaxWidth="400"

VerticalScrollBar :

VerticalScrollBarVisibility="Auto" MaxHeight="200"

HorizontalScrollBar :

HorizontalScrollBarVisibility="Auto" MaxWidth="400"

生死何惧 2024-08-10 05:03:03

适合我的解决方案。这个想法取自 在这里。
XAML中定义

            <RichTextBox x:Name="PART_rtb" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Auto" TextChanged="MyRichTextBox_OnTextChanged">
                <RichTextBox.Document>
                    <FlowDocument x:Name="PART_fd"  >
                        <FlowDocument.Resources>
                            <!--This style is used to set the margins for all paragraphs in the FlowDocument to 0.-->
                            <Style TargetType="{x:Type Paragraph}">
                                <Setter Property="Margin" Value="3"/>

                            </Style>
                        </FlowDocument.Resources>
                    </FlowDocument>
                </RichTextBox.Document>
            </RichTextBox>

我在代码中的

   private void MyRichTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        double i  = PART_rtb.Document.GetFormattedText().WidthIncludingTrailingWhitespace + 20;
        (sender as RichTextBox).Document.PageWidth = i;
    }

Suitable solution for me. The idea was taken from here.
I defined in XAML

            <RichTextBox x:Name="PART_rtb" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Auto" TextChanged="MyRichTextBox_OnTextChanged">
                <RichTextBox.Document>
                    <FlowDocument x:Name="PART_fd"  >
                        <FlowDocument.Resources>
                            <!--This style is used to set the margins for all paragraphs in the FlowDocument to 0.-->
                            <Style TargetType="{x:Type Paragraph}">
                                <Setter Property="Margin" Value="3"/>

                            </Style>
                        </FlowDocument.Resources>
                    </FlowDocument>
                </RichTextBox.Document>
            </RichTextBox>

In Code

   private void MyRichTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        double i  = PART_rtb.Document.GetFormattedText().WidthIncludingTrailingWhitespace + 20;
        (sender as RichTextBox).Document.PageWidth = i;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文