It is a little unintuitive, but it can be done. This is a two-step process. Firstly, what do you want to use the TransformToVisual
function ( from MSDN ):
Returns a transform object that can be used to convert coordinates from a UIElement to the specified object.
TransformToVisual
will give a GeneralTransform
that will do the conversion from any UIElement to any other UIElement (given that they both exist in the same visual tree). It looks like you want to convert from RootVisual
.
var transform = Application.RootVisual.TransformToVisual(myUiElement);
The transform
object is now a general transformation that can be used to transform something in the same way that myUiElement
was converted relative to RootVisual
The next step is to transform the points using this transform.
var myUiElementPosition = transform.Transform(new Point(0,0));
myUiElementPosition
now the Point
that has been converted and should be the UIElement
position you are looking for. new Point(0,0)
used because I assume that you want the position to be relative to the upper left corner of RootVisual
.
source share