如何获得跑步或段落的身高 [英] How to get the HEIGHT of the Run or Paragraph

查看:100
本文介绍了如何获得跑步或段落的身高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FlowDocument中找到了RunParagraph,现在我需要知道它的 HEIGHT .

I found the Run or Paragraph in FlowDocument and now I need to know the HEIGHT of it.

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

实际上有可能吗?

谢谢!

推荐答案

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

A little function i am using. The input is a string containing a Section. You can easily render other blockelements like Paragraph.

您还可以省略Parse方法的第二个参数.

You also can omit the second parameter of the Parse method.

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

The trick is not to measure the Paragraph, but the ViewBox which contains a RichTextBox. This is needed to actually render the Flowdocument. The ViewBox dynamically gets the size of the rtb. Maybe you even can do this without the ViewBox. I spent some time to figure this out and it works for me.

请注意,RichTextBox中的Width被设置为double.MaxValue.这意味着当您要测量单个段落时,它必须很长或所有内容都在一行中.因此,这仅在您知道输出设备的宽度时才有意义.因为这是一个FlowDocument,所以没有宽度,它会流动;) 我用它来在我知道纸张尺寸的情况下对FlowDocument进行分页.

Note that Width of the RichTextBox is set to double.MaxValue. This means when you want to measure a single paragraph it has to be very long or everything is in one line. So this only makes sense when you know the Width of your output device. As this is a FlowDocument there is no Width, it flows ;) I use this to paginate a FlowDocument where i know the paper size.

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

The returned Height is device independent units.

private double GetHeaderFooterHeight(string headerFooter)
        {

            var section = (Section)XamlReader.Parse(headerFooter, _pd.ParserContext);
            var flowDoc = new FlowDocument();
            flowDoc.Blocks.Add(section);

            var richtextbox = new RichTextBox { Width = double.MaxValue, Document = flowDoc };
            var viewbox = new Viewbox { Child = richtextbox };

            viewbox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            viewbox.Arrange(new Rect(viewbox.DesiredSize));

            var size = new Size() { Height = viewbox.ActualHeight, Width = viewbox.ActualWidth };

            return size.Height;
        }

这篇关于如何获得跑步或段落的身高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆