如何获取 WPF 视觉对象的子级的边界框(不包括父级)
从 VisualTreeHelper.GetDescendantBounds() 的 MSDN 文档中:
// Return the bounding rectangle of the parent visual object and all of its descendants.
Rect rectBounds = VisualTreeHelper.GetDescendantBounds(parentVisual);
我得到了这个并且它有效,但我不想想要包含父级的边界,原因是我的父级是 XPS 文档的页面,因此调用它只会返回页面边界,这不是我想要的。我想要页面上所有内容的边界框,即页面视觉对象的子级的边界框。
// snippet of my code
Visual visual = paginator.GetPage(0).Visual;
Rect contentBounds = VisualTreeHelper.GetDescendantBounds(visual);
// above call returns the page boundaries
// is there a way to get the bounding box of just the children of the page?
我需要这个的原因是我将 XPS 页面保存到位图,并且需要包含尽可能少的空白,以将位图限制为仅页面的“已使用”区域。
我是否需要自己迭代视觉对象的所有子级并调用 VisualTreeHelper.GetContentBounds( ) 每一个?我认为有比这样做更好的方法......
From the MSDN documentation for VisualTreeHelper.GetDescendantBounds():
// Return the bounding rectangle of the parent visual object and all of its descendants.
Rect rectBounds = VisualTreeHelper.GetDescendantBounds(parentVisual);
I get this and it works, but I do not want to include the parent's bounds, the reason is that my parent is a page of an XPS document, and so calling this just returns the page boundaries, which is not what I want. I want the bounding box of everything on the page, i.e. just of the children of the page visual.
// snippet of my code
Visual visual = paginator.GetPage(0).Visual;
Rect contentBounds = VisualTreeHelper.GetDescendantBounds(visual);
// above call returns the page boundaries
// is there a way to get the bounding box of just the children of the page?
The reason I need this is that I'm saving the XPS page to a bitmap and need to include as little white space as possible, to limit the bitmap to only the 'used' area of the page.
Do I need to iterate over all the children of the visual myself and call VisualTreeHelper.GetContentBounds() on each one? I thought there would be a better way than doing this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过枚举父(页面)视觉对象的所有子视觉对象提出了一个可行的解决方案。更高效和/或库解决方案会更好,但这目前有效。
I've come up with a workable solution by enumerating over all the child visuals of the parent (page) visual. A more efficient and/or library solution would be better, but this works for now.