So I am building a flowdocument paragraph by paragraph, and I was wondering if there was a way that I could measure the height of a block at a given time.
my code looks something like this:
section s = new section();
block b1 = new Block(new Run("Text here"));//add height to total block height
block b2 = new Block(new Run("Text here"));//add height to total block height
block b3 = new Block(new Run("Text here"));//add height to total block height
block b4 = new Block(new Run("Text here"));//add height to total block height
s.blocks.add(b1);s.Blocks.Add(b2)...;s.blocks.add(b4)
//measure section here
FlowDocument f = new FlowDocument;
f.Blocks.Add(s);
I could either measure each paragraph after it is a开发者_如何学Pythondded, and keep a running tally,
OR
I could measure the whole section, after all the blocks have been added to it.
is it possible?
thanks!
The FlowDocument model, based on FrameworkContentElement not FrameworkElement, does not inherit such goodness as Visibility, Height and Width settings.
The only way I have found to force measuring, only when there was no other way, was to inject UIElements into the document using the BlockUIContainer. The named UIElement could then be measured at run time. It is, to be honest, a bit of an ugly hack, but I have not found another way to this point.
You could do it like this.
Block b1 = new Block(new Run("Text here"));
double width = .0;
Rect r = b1.ElementStart.GetCharacterRect(LogicalDirection.Forward);
//add height to total block height
width += r.Height;
The same method can be used to get the height of even the Section
. I'm still searching for a faster solution though, but this one works just fine. I'll keep you posted if I find anything else. :)
精彩评论