WPF FlowDocument:高度等的力计算. [英] WPF FlowDocument: force calculation of height etc. "off screen"

查看:318
本文介绍了WPF FlowDocument:高度等的力计算.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标:一个DocumentPaginator,它带有一个带有表的FlowDocument,该表将表拆分成适合页面大小并在每个页面上重复页眉/页脚(特殊标记的TableRowGroups).

My target: a DocumentPaginator which takes a FlowDocument with a table, which splits the table to fit the pagesize and repeat the header/footer (special tagged TableRowGroups) on every page.

要拆分表格,我必须知道其行的高度.

For splitting the table I have to know the heights of its rows.

在通过代码构建FlowDocument-table时,TableRows的高度/宽度为0(当然).如果我将此文档分配给FlowDocumentScrollViewer(已设置PageSize),则将计算高度等.不使用UI绑定的对象,这可能吗?实例化未绑定到窗口的FlowDocumentScrollViewer不会强制对高度进行分页/计算.

While building the FlowDocument-table by code, the height/width of the TableRows are 0 (of course). If I assign this document to a FlowDocumentScrollViewer (PageSize is set), the heights etc. are calculated. Is this possible without using an UI-bound object? Instantiating a FlowDocumentScrollViewer which is not bound to a window doesn't force the pagination/calculation of the heights.

这就是我确定TableRow的高度的方法(它非常适合FlowDocumentScrollViewer显示的文档):

This is how I determine the height of a TableRow (which works perfectly for documents shown by a FlowDocumentScrollViewer):

        FlowDocument doc = BuildNewDocument();
        // what is the scrollviewer doing with the FlowDocument?
        FlowDocumentScrollViewer dv = new FlowDocumentScrollViewer();
        dv.Document = doc;
        dv.Arrange(new Rect(0, 0, 0, 0));

        TableRowGroup dataRows = null;
        foreach (Block b in doc.Blocks)
        {
          if (b is Table)
          {
            Table t = b as Table;
            foreach (TableRowGroup g in t.RowGroups)
            {
              if ((g.Tag is String) && ((String)g.Tag == "dataRows"))
              {
                dataRows = g;
                break;
              }
            }
          }
          if (dataRows != null)
            break;
        }
        if (dataRows != null)
        {
          foreach (TableRow r in dataRows.Rows)
          {
            double maxCellHeight = 0.0;
            foreach (TableCell c in r.Cells)
            {
              Rect start = c.ElementStart.GetCharacterRect(LogicalDirection.Forward);
              Rect end = c.ElementEnd.GetNextInsertionPosition(LogicalDirection.Backward).GetCharacterRect(LogicalDirection.Forward);
              double cellHeight = end.Bottom - start.Top;
              if (cellHeight > maxCellHeight)
                maxCellHeight = cellHeight;
            }
            System.Diagnostics.Trace.WriteLine("row " + dataRows.Rows.IndexOf(r) + " = " + maxCellHeight);
          }
        }

我将FlowDocumentScrollViewer添加到我的示例中. 排列"的调用会强制FlowDocument计算其高度等.我想知道FlowDocumentScrollViewer对FlowDocument所做的事情,因此无需UIElement就可以做到.有可能吗?

I added the FlowDocumentScrollViewer to my example. The call of "Arrange" forces the FlowDocument to calculate its heights etc. I would like to know, what the FlowDocumentScrollViewer is doing with the FlowDocument, so I can do it without the UIElement. Is it possible?

推荐答案

我的猜测不会,没有UIElement便无法做到.

My guess would be no, you can't do it without a UIElement.

FlowDocument本身实际上并不呈现任何内容.看一下相关器中的类型,看起来它只是一个数据类型.它就像拥有一个字符串,并且想知道渲染时的大小...如果不进行某种度量传递就无法真正做到这一点.

FlowDocument, by itself, doesn't actually render anything. Looking at the type in relector it looks like it is just a data type. Its about like having a string and wanting to know its size when rendered ... can't really do it without doing some kind of measure pass.

我不确定,但是您可以通过传递Double.PositiveInfinity而不是0来获得更好的性能,至少您不必担心测量n行休息.

I don't know for sure, but you might get better performance in the arrange pass by passing in Double.PositiveInfinity for the size rather than 0. At least then it won't have to worry about measuring 'n' line breaks.

这篇关于WPF FlowDocument:高度等的力计算.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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