确定 WPF 元素相对于某个父元素的边界矩形
我认为这是一个非常简单的请求,但我似乎无法在搜索中找到确凿的答案。如何确定窗口中特定视觉元素相对于其他父元素的边界?
我尝试过使用 LayoutInformation.GetLayoutSlot ,但这似乎只是返回 0,0 处的 Rect ,并且不反映元素的实际位置。
我想做的是使用 RenderTargetBitmap 截取窗口的“屏幕截图”,然后将其裁剪到特定元素,但我无法获取元素的边界来知道裁剪位图的内容到!
I consider this a pretty simple request, but I can't seem to find a conclusive answer in my searches. How can I determine the bounds of a particular visual element in my window, relative to some other parent element?
I've tried using LayoutInformation.GetLayoutSlot
but this just seems to return a Rect
at 0,0 and doesn't reflect the actual location of the element.
What I'm trying to do is take a "screenshot" of a window using RenderTargetBitmap
and then crop it to a particular element, but I can't get the element's bounds to know what to crop the bitmap to!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很简单:
事实上,将其放在单独的方法中可能有点过分了。
It is quite simple:
In fact it may be overkill to put it in a separate method.
LayoutSlot 选项对我来说根本不起作用。
这最终给了我一个相对于指定父/祖先控件的子位置:
The LayoutSlot option didn't work for me at all.
This ended up giving me a child position relative to a specified parent/ancestor control:
考虑到我在这里找到的一些建议,这解决了我的问题。
Taking into account a few suggestions i found here this solved the problem for me.
没关系,我最终设法使用
LayoutInformation.GetLayoutSlot()
的组合来解决它(尽管我可能可以使用ActualWidth
/ActualHeight
或RenderSize
)和UIElement.TranslatePoint()
。看起来是一个相当复杂的解决方案,其实它可以像这样简单:
哦,好吧。也许是时候采用扩展方法了。 :)
Nevermind, I finally managed to figure it out using a combination of
LayoutInformation.GetLayoutSlot()
(although I probably could have used eitherActualWidth
/ActualHeight
orRenderSize
) andUIElement.TranslatePoint()
.Seems a rather complicated solution when it could be as simple as this:
Oh well. Maybe time for an extension method. :)