哎呀,你是对的,试试这个:
将窗口连接到以下事件(我通过简单的按钮单击完成了此操作)
var childWindow = new ChildWindow();
childWindow.Closing += new EventHandler<CancelEventArgs>(OnChildWindowClosing);
childWindow.Show();
现在您需要做的是遍历 ChildWindow PARTS DOM 并找到将为您提供位置的 ContentRoot。
static void OnChildWindowClosing(object sender, CancelEventArgs e)
{
var childWindow = (ChildWindow)sender;
var chrome = VisualTreeHelper.GetChild(childWindow, 0) as FrameworkElement;
if (chrome == null) return;
var contentRoot = chrome.FindName("ContentRoot") as FrameworkElement;
if (contentRoot == null || Application.Current == null || Application.Current.RootVisual == null) return;
var gt = contentRoot.TransformToVisual(Application.Current.RootVisual);
if (gt == null) return;
var windowPosition = gt.Transform(new Point(0, 0));
MessageBox.Show("X:" + windowPosition.X + " Y:" + windowPosition.Y);
}
HTH。