未设置宽度的 WPF RichTextBox
我有以下 XAML 代码:
<Window x:Class="RichText_Wrapping.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Grid>
<RichTextBox Height="100" Margin="2" Name="richTextBox1">
<FlowDocument>
<Paragraph>
This is a RichTextBox - if you don't specify a width, the text appears in a single column
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
...如果您在 XAML 中创建此窗口,您可以看到,当您没有指定窗口的宽度时,它会将文本包装在单列中,一次一个字母。 我缺少什么吗? 如果这是已知的控制缺陷,是否有任何解决方法?
I have the following XAML code:
<Window x:Class="RichText_Wrapping.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Grid>
<RichTextBox Height="100" Margin="2" Name="richTextBox1">
<FlowDocument>
<Paragraph>
This is a RichTextBox - if you don't specify a width, the text appears in a single column
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
... If you create this window in XAML, you can see that when you don't specify a width for the window, it wraps the text in a single column, one letter at a time. Is there something I'm missing? If it's a known deficiency in the control, is there any workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是 WPF RichTextBox 中已确认的错误。 要修复此问题,请将 FlowDocument 的 PageWidth 绑定到 RichTextBox 宽度,即
编辑:
为 FlowDocument 指定一个名称,以便您可以在代码隐藏中访问它,并且永远不会在代码隐藏中新建流程文档。
This is a confirmed bug with the WPF RichTextBox. To fix it, Bind the PageWidth of the FlowDocument to the RichTextBox width, i.e.
EDIT:
Give the FlowDocument a name so that you can access it in the code behind and never new the flow document in codebehind.
尝试将 FlowDocument 的宽度(一种方式)绑定到容器 RichTextBox 的宽度。
为我工作...
Try binding the FlowDocument's width (one way) to the width of the container RichTextBox.
Worked for me...
本文中的方法为我工作:
The approach in this article worked for me:
我复制粘贴了您的代码,但它不在单列中,您的某个地方的宽度是否很小? 例如,可能在后面的代码中定义。
I copy pasted your code and its not in a single column, Do you have a width somewhere that is small? Maybe defined on the code behind for instance.
我注意到,只有当我的默认
ScrollViewer
样式显式设置HorizontalScrollBarVisibility=Hidden
时,我才会遇到此问题。删除此设置器(默认值是
Hidden
)解决了我的RichTextBox
中的单列问题。I noticed that I only had this issue when my default
ScrollViewer
style explicitly setHorizontalScrollBarVisibility=Hidden
.Removing this setter (default value is
Hidden
anyway) fixed the single column issue for me in myRichTextBox
.只是为了记录,因为我认为这个线程缺少一些解释,根据原因:RichTextBox MeasureOverride 实现就是这样。 我不会称其为错误,也许只是一个糟糕的设计行为,因为正如上面提到的,FlowDocument 由于其复杂性而测量起来并不便宜。 最重要的是,通过绑定 MinWidth 或将其包装在限制容器中来避免无限的宽度约束。
Just for the record as I think this thread is missing some explanations as per the why: RichTextBox MeasureOverride implementation is like that. I won't call that a bug, maybe just a poor design behavior justified by the fact that just like mentioned above the FlowDocument is not cheap to measure due to its complexity. Bottom line, avoid unlimited Width constraint by binding MinWidth or wrap it in a limiting container.