WPF FlowDocument: force calculation of height etc. “off screen”

ⅰ亾dé卋堺 提交于 2019-12-12 17:42:07

问题


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.

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.

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);
          }
        }

Edit: 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?


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/2000177/wpf-flowdocument-force-calculation-of-height-etc-off-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!