I have a Datagrid and I want to know the position of the datacell to overlay it on the window.
It works great with just one monitor, but with multiple monitors the window shifts.
Here is the code:
Point point = cell.PointToScreen(new Point(0, 0)); ... Window myWindow = new Window(); myWindow.Top = point.Y; myWindow.Left = point.X;
Does anyone have experience positioning on multiple monitors?
EDIT:
I did the following test:
public MyWindow() { ... this.LocationChanged += MyWindow_LocationChanged; } void MyWindow_LocationChanged(object sender, EventArgs e) { Console.WriteLine(this.Top + " <--> " + this.PointToScreen(new Point(0, 0)).Y); }
Results:
- Single-Monitor: 0 β 30; 20 β 50; 100 β 130 & ==> There is always a difference of 30 (may be caused by the header)
- Dual monitor: 0 β 30; 20 β 55; 100 β 153 & ==> 0.0 times the difference is 30. But the more I moved the window from 0.0. the greater the difference becomes, but it must remain unchanged. Very strange!
EDIT2:
Here is my solution, thanks to CodeNaked for a hint:
Point point = cell.PointToScreen(new Point(0, 0)); ... Window myWindow = new Window(); PresentationSource source = PresentationSource.FromVisual(this); myWindow.Top = point.Y / source.CompositionTarget.TransformToDevice.M22; myWindow.Left = point.X / source.CompositionTarget.TransformToDevice.M11;
source share