【问题标题】:WPF FlowDocument: force calculation of height etc. "off screen"WPF FlowDocument:强制计算高度等“屏幕外”
【发布时间】:2010-01-04 15:23:28
【问题描述】:

我的目标:一个 DocumentPaginator,它接受一个带有表格的 FlowDocument,它拆分表格以适应页面大小并在每个页面上重复页眉/页脚(特殊标记的 TableRowGroups)。

为了拆分表格,我必须知道其行的高度。

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

这就是我确定 TableRow 高度的方式(这对于 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 的情况下做到这一点。有可能吗?

【问题讨论】:

    标签: wpf pagination flowdocument


    【解决方案1】:

    我的猜测是否定的,如果没有 UIElement,您将无法做到这一点。

    FlowDocument 本身实际上并不渲染任何东西。查看 rector 中的类型,它看起来只是一种数据类型。它就像拥有一个字符串并想在渲染时知道它的大小......如果不进行某种测量传递,就无法真正做到这一点。

    我不确定,但您可能会通过为大小而不是 0 传递 Double.PositiveInfinity 在排列传递中获得更好的性能。至少那时它不必担心测量“n”行休息。

    【讨论】:

    • 调用高度或宽度为无穷大的 Arrange 将引发异常。你必须传递一个有效的数字。我想我必须使用 FlowDocumentScrollViewer,但如果您知道目标页面大小(如“letter”或“DIN A4”),则渲染应该与 UI 无关......
    猜你喜欢
    • 2018-12-14
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多