'Getting current page of a FlowDocumentPageViewer
I have a FlowDocumentPageViewer control that I am populating from the code behind. I am adding text from 3-4 items. I need to know on what page item 1,2,3... ends.
These items get added to a FlowDocument in a loop and then at the end I have
flowDocumentPageViewer.Document = resultsDocument;
However, even then if I call
flowDocumentPageViewer.PageCount
I still get 0. If I do some event like a click and print the above PageCount after the window renders, it does print the correct value. However, I need a running total of this value as I populate the FlowDocument. I am hoping someone knows some kind of trick with text metrics or any other way that this could be solved.
Solution 1:[1]
You have no choice but to trigger a (virtual) render of your control to get this kind of info.
the easiest way is to force a updateLayout()
on the FlowDocumentPageViewer, but this means it'll show the result before you get the info which is probably not what you want.
you can also use the Arrange()
method if you don't want the content to be shown:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.arrange.aspx
after using this on your control you should get the updated info.
Solution 2:[2]
I had the same problem and to be honest I still do. Any updateLayout() did not help. Now I use a timer to get the pagecount a second later after I assigend to document to the viewer. Works fine.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | David |
Solution 2 | TalkingCode |