WPF - 计算 FlowDocument 的宽度
我正在使用 C# 和 VS2008 工作。
我有一个包含 FlowDocument 的 WPF 应用程序,其中包含一个段落,其中包含许多相当短的行(即 Spans 和 LineBreaks)。 然而,线路的长度各不相同。 我希望 FlowDocument 的宽度足够大,以容纳最长的这些行而不换行,但不能更宽,以免浪费空间。
将其放入 Width=Auto 的网格列中不起作用:FlowDocument 始终消耗允许的最大宽度,因为 FlowDocument 可以有效地换行以适应许多不同的宽度。
我可以在代码中生成这些行时遍历这些行,并计算出每行的宽度,但我在 Span 或 Run 类上没有看到 Width 属性。
关于如何自动或手动设置此文档的宽度有什么想法吗?
我使用了 AndrewS 的建议想法,从 FlowDocument 转移到了 StackPanel,每个标签都包含一行。 XAML 标记是
<Border BorderThickness="2" Margin="2" BorderBrush="Black">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<StackPanel
Orientation="Vertical"
x:Name="itemsStackPanel" />
</ScrollViewer>
</Border>
代码创建跨度并添加它们,如下所示:
Label itemLabel = new Label();
// pad left and right, keep close top and bottom
itemLabel.Padding = new Thickness(2,0,2,0);
itemLabel.Content = contentSpan;
this.itemsStackPanel.Children.Add(itemLabel);
大小现在很完美,唯一的问题是字体看起来与旁边的 FlowDocument 不完全相同,尽管字体 Family 和 Font Size 是相同(Segoe UI,15pt),并且标签看起来比 FlowDocument 中的线条相距更远。 我已将其作为一个单独的问题提出
I am working in C# and VS2008.
I have a WPF application containing a FlowDocument, which contains a paragraph, which contains a number of fairly short lines (i.e. Spans and LineBreaks). However the length of lines varies. I would like the FlowDocument's width to be large enough to accommodate the longest of these lines without wrapping, but no wider so as to not waste space.
Putting it into a grid column with Width=Auto doesn't work: the FlowDocument always consumes the max width allowed, since the FlowDocument can validly wrap to suit a number of different widths.
I could go through the lines as I generate them in code, and work out a width for each one, but I see no Width property on the Span or Run classes.
Any ideas on how to automatically or manually set a width for this document?
I used AndrewS's suggested idea and moved from a FlowDocument to a StackPanel of labels each containing a line. The XAML Markup is
<Border BorderThickness="2" Margin="2" BorderBrush="Black">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<StackPanel
Orientation="Vertical"
x:Name="itemsStackPanel" />
</ScrollViewer>
</Border>
The code creates spans and adds them as follows:
Label itemLabel = new Label();
// pad left and right, keep close top and bottom
itemLabel.Padding = new Thickness(2,0,2,0);
itemLabel.Content = contentSpan;
this.itemsStackPanel.Children.Add(itemLabel);
Sizing is perfect now, and the only snag is that the font doesn't look exactly the same as the FlowDocument next to it, despite the font Family and Font Size being the same (Segoe UI, 15pt), and that the Labels look much further apart than the lines in the FlowDocument. I have asked this as a separate question
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以用不同类型的元素替换线条,例如 TextBlocks 或 Labels(您可以控制换行)或“专为固定格式文档演示和打印场景而设计”的 Glyphs。
我真的不认为你想尝试测量线条并强制文档宽度。
You could replace the lines with a different type of element such as TextBlocks or Labels (for which you can control wrapping) or Glyphs which "are designed for fixed-format document presentation and print scenarios".
I really don't think you want to try measuring lines and forcing the document width.
我也有类似的情况,但我确实想测量线宽。 遗憾的是,RichTextBox 有两个重要的功能捆绑在一起 - 运行和格式中的文本前景色和背景色。 我需要颜色并且不太关心格式。 我有一个字符“网格”,我想在字符上运行几个“光标”。 我计划将背景颜色实现为单独的光标,并使用前景色进行语法着色。 它是一种固定宽度的字体,我不希望网格换行,因此基本上没有格式。 我想我可以将其实现为一个网格,我在运行时根据文本的大小设置行和列,但这似乎是真正的资源浪费 - 特别是如果文件非常大 - 即,一个 100x100 网格每个单元格中都有一个单独的标签。 最初,也许有点天真,我认为使用 RichTextBox 会很容易,但我开始相信事实并非如此。 另一种可能性是将画布放在标准文本框的顶部并自己绘制矩形,但必须在内容滚动等时处理所有内容,这对我来说听起来很丑陋且容易出错。 我现在的代码是:
正如我在评论中所说,这似乎适用于我当前的情况,但我还没有真正与其他许多人一起测试过它,所以我不确定它是否普遍有效,而且它只是一个大的问题无论如何,我无法解释,所以我想知道如何正确处理这个问题。 感谢您的任何回复。 哦,我应该指出,正如我上面所说, for 是固定音高的,并且所有传入的字符串的长度都与合同规定的相同,因此所有行的长度都应该完全相同。
I've got a similar situation, but I do want to measure the line width. Sadly, the RichTextBox has two important features bundled together - text foreground and background colors in runs and formatting. I need the colors and could care less about the formatting. I've got a "grid" of characters and I want to run several "cursors" over the characters. I plan on implementing the background colors as the individual cursors and use foreground colors for syntax coloring. It's a fixed width font and I don't want the grid to wrap so essentially no formatting. I suppose I could implement this as a grid that I set the rows and columns at runtime according to how big my text is, but that seems like a real waste of resources - especially if the file is really large - i.e., a 100x100 grid with a separate label in every cell. Originally, and perhaps a little naively, I thought that it would be a snap with RichTextBox but I'm beginning to believe otherwise. Another possibility is to put a canvas on top of a standard TextBox and drawing rects myself but having to deal with everything as stuff scrolls, etc. sounds ugly and error prone to me. My code right now is:
As I state in the comments, this seems to work for my current case, but I haven't really tested it with many others so I'm not sure whether it works in general, plus it's just a big kludge anyway that I can't explain so I'd like to know how to handle this correctly. Thanks for any responses. Oh, I should point out as I stated above, that the for is fixed pitch and all the incoming strings are the same length as enforced by the contract so all lines should be exactly the same length.