It should be sufficient to write
var p = ctrl.PointToScreen(new Point());
Left = p.X;
Top = p.Y;
Question
I'm trying to place a window in a way which its top-left corner will be in the same point of a specific WPF control op-left corner.
So I tried getting the control's screen coordinates like this:
PresentationSource source = PresentationSource.FromVisual(ctrl);
Point posFromScreen = ctrl.PointToScreen(new Point(0, 0));
Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(posFromScreen);
and then I set the Top
and Left
coordinates of the window to targetPoints
.
But My window always appears to the RIGHT of the control instead of overlapping with him in the top-left corner.
What am I doing wrong? How can it be done?
Solution
It should be sufficient to write
var p = ctrl.PointToScreen(new Point());
Left = p.X;
Top = p.Y;