请帮助我。
public myChildWindow()
{
InitializeComponent();
// set left and top from saved values
Margin = new Thickness(70, 50, 0, 0);
}
private void ChildWindow_Closed(object sender, EventArgs e)
{
// How to know the position of the ChildWindow when you close it ?
// get left and top for save values
...
}
答案 0 :(得分:5)
哎呀你是对的,试试这个:
将窗口连接到以下事件(我通过简单的按钮单击完成此操作)
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。
答案 1 :(得分:0)
如果您订阅了Closing事件而未关闭,则可以从Window中找到Left / Top值
即:
private void Button_Click(object sender, RoutedEventArgs e)
{
LoginWindow loginWnd = new LoginWindow();
loginWnd.Closing += new EventHandler(loginWnd_Closing);
}
然后获取位置值:
double x = GetValue(ChildWindow.LeftProperty) as double;
double y = GetValue(ChildWindow.TopProperty) as double;