2

我的目标:一个 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 的情况下做到这一点。可能吗?

4

1 回答 1

0

我的猜测是否定的,没有 UIElement 就无法做到。

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

我不确定,但你可以通过传入 Double.PositiveInfinity 的大小而不是 0 来在排列过程中获得更好的性能。至少这样它就不必担心测量“n”个换行符。

于 2010-04-04T06:03:54.267 回答