有没有办法让我测量 WPF flowdoc 中块/部分的高度?

发布于 2024-11-30 23:38:33 字数 660 浏览 0 评论 0原文

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

我的代码看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的她 2024-12-07 23:38:33

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.

握住我的手 2024-12-07 23:38:33

你可以这样做。

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 的高度。我仍在寻找一种更快的解决方案,但这个效果很好。如果我发现任何其他情况,我会随时通知您。 :)

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文