我正在尝试在 WPF RichTextBox 控件中显示大量数据。 我的数据包含空格字符。有一个默认的自动换行行为,不允许将“单词”拆分并显示在更多行上。
此行为是由空格字符、问号、句号或任何其他句子/单词分隔符触发的。在下面的示例中,如果将空格字符替换为字母(例如:“X”),则所有内容都将按预期显示。由于未找到分隔符,因此允许将大“单词”截断并显示在多行中。
有没有办法禁用这种单词/句子换行行为?
这是 XAML 代码:
<Window x:Class="StackOverQuestion_TextBoxWrap.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="535">
<Grid>
<RichTextBox Name="RichTextBox" />
</Grid>
</Window>
这是背后的cs代码:
public MainWindow()
{
InitializeComponent();
Random rnd = new Random();
RichTextBox.FontFamily = new System.Windows.Media.FontFamily( "Lucida Console" );
Paragraph par = new Paragraph();
for ( int i = 0 ; i < 6000 ; i++ )
{
Run run = new Run();
run.Text = rnd.NextDouble().ToString() + " " ;
par.Inlines.Add( run );
}
RichTextBox.Document.Blocks.Add( par );
}
不良的换行行为:(请注意线条的不同长度)
0.562230281327958 0.269015421750497 0.130114109315963 0.527640242375266 0.592048898149305
0.73868335026255 0.478530279117883 0.939313878276997 0.890535918479104 0.00047110533363703
0.546423877378192 0.780972927241108 0.697112546626997 0.66897076306351 0.634957212319112
0.498651245375467 0.808829494662969
所需的换行行为:(请注意线条的长度相同)
0.562230281327958 0.269015421750497 0.130114109315963 0.527640242375266 0.592048898149305
0.73868335026255 0.478530279117883 0.939313878276997 0.890535918479104 0.0004711053336370
3 0.546423877378192 0.780972927241108 0.697112546626997 0.66897076306351 0.63495721231911
2 0.498651245375467 0.808829494662969
最佳答案
我认为您需要禁用 RichTextBox 控件中的自动换行功能,根据 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.
没有显式属性可以禁用它,您可以执行以下操作:
richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
richTextBox1.Document.PageWidth = 1000;
按照建议here .
关于c# - WPF RichTextBox 自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19589663/