如何获得Run或Paragraph的高度

时间:2013-04-12 18:16:48

标签: c# .net wpf height flowdocument

我在Run找到了ParagraphFlowDocument,现在我需要知道它的 HEIGHT

while (navigator.CompareTo(flowDocViewer.Document.ContentEnd) < 0)
  {
      TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
      Run run = navigator.Parent as Run;
      // I need to get HEIGHT of Run in pixels somehow

事实上可以吗?

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:6)

我正在使用的一个小功能。输入是包含Section的字符串。您可以轻松渲染其他块元素,如段落。

您也可以省略Parse方法的第二个参数。

诀窍不是测量段落,而是包含RichTextBox的ViewBox。这是实际呈现Flowdocument所必需的。 ViewBox动态获取rtb的大小。也许你甚至可以在没有ViewBox的情况下做到这一点。我花了一些时间来解决这个问题,它对我有用。

请注意Width的{​​{1}}设置为RichTextBox。这意味着当您想要测量单个段落时,它必须非常长或者所有内容都在一行中。所以这只有在你知道输出设备的宽度时才有意义。由于这是一个FlowDocument没有宽度,它流动;) 我使用它来分页FlowDocument,我知道纸张大小。

返回的高度是与设备无关的单位。

double.MaxValue