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!
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. :)
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.
It is quite simple:
In fact it may be overkill to put it in a separate method.