[英]C#/WPF: Disable Text-Wrap of RichTextBox
Does anyone know how I can disable the text wrapping of a RichTextBox
?有谁知道如何禁用
RichTextBox
的文本换行? Eg 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.例如,如果我有一个不适合窗口的大字符串,
RichTextBox
放置不能在新行中显示的字符串部分。 I want to disable that (and make it visible only by using the Scrollbar
).我想禁用它(并仅通过使用
Scrollbar
使其可见)。
Thanks a lot.非常感谢。
Cheers干杯
A RichTextBox
in WPF is simply an editor for a FlowDocument
. WPF 中的
RichTextBox
只是FlowDocument
的编辑器。
According to MSDN :根据MSDN :
Text always wraps in a RichTextBox .
文本始终包含在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 .
如果您不希望文本换行,则将FlowDocument上的PageWidth设置为大于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:因此,虽然您无法明确禁用
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 = false
to work precisely as OP seems to desire.注意(截至 2015 年 7 月):VS2015 RC 允许
wordwrap = false
完全按照 OP 的要求工作。 I believe earlier versions of Visual Studio did also.我相信早期版本的 Visual Studio 也是如此。
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>
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.问题是关于取决于文本长度和刷新频率的性能。
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.我还需要显示一个大字符串并尝试了 RichTextBox,但我不喜欢将文档的 PageWidth 设置为固定大小的解决方案。 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.如果 TextBlock 就足够了,您可以改用它并将其放置在 ScrollViewer 中。 It worked perfect for me since I did not need all the extra features of the RichTextBox.
它非常适合我,因为我不需要 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>
VerticalScrollBar :垂直滚动条:
VerticalScrollBarVisibility="Auto" MaxHeight="200" VerticalScrollBarVisibility="Auto" MaxHeight="200"
HorizontalScrollBar :水平滚动条:
HorizontalScrollBarVisibility="Auto" MaxWidth="400" HorizontalScrollBarVisibility="Auto" MaxWidth="400"
Suitable solution for me.适合我的解决方案。 The idea was taken from here.
这个想法来自here。 I defined in XAML
我在 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.