有没有办法测量WPF flowdoc中块/节的高度?

时间:2011-08-23 23:08:01

标签: c# wpf flowdocument

所以我逐段构建一个flowdocument,我想知道是否有一种方法可以在给定时间测量块的高度。

我的代码看起来像这样:

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

我可以在添加后测量每个段落,并保持运行记录,

OR

在添加了所有块之后,我可以测量整个部分。

有可能吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

基于FrameworkContentElement而不是FrameworkElement的FlowDocument模型不会继承可见性,高度和宽度设置等优点。

我发现强制测量的唯一方法是,只有在没有其他方法时,才能使用BlockUIContainer将UIElements注入文档。然后可以在运行时测量命名的UIElement。说实话,这是一个丑陋的黑客,但我还没有找到另一种方法。

答案 1 :(得分:0)

你可以这样做。

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;

可以使用相同的方法获得偶数Section的高度。我仍然在寻找更快的解决方案,但这个工作正常。如果我发现其他任何事情,我会随时发布。 :)