有没有办法让我测量 WPF flowdoc 中块/部分的高度?
所以我正在逐段构建一个流程文档,我想知道是否有一种方法可以测量给定时间块的高度。
我的代码看起来像这样:
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);
我可以在添加每个段落后测量它,并保持运行计数,
或者
我可以在添加所有块后测量整个部分。
是否可以?
谢谢!
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 added, 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FlowDocument 模型基于 FrameworkContentElement 而不是 FrameworkElement,不会继承 Visibility、Height 和 Width 设置等优点。
我发现强制测量的唯一方法(仅当没有其他方法时)是使用 BlockUIContainer 将 UIElements 注入文档中。然后可以在运行时测量指定的 UIElement。老实说,这是一个有点丑陋的黑客,但到目前为止我还没有找到其他方法。
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.
你可以这样做。
甚至可以使用相同的方法来获取
Section
的高度。我仍在寻找一种更快的解决方案,但这个效果很好。如果我发现任何其他情况,我会随时通知您。 :)You could do it like this.
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. :)