如何在关闭Silverlight ChildWindow时知道它的位置

1 silverlight position childwindow

请帮我.

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
    ...
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*nes 5

哎呀你是对的,试试这个:

将窗口连接到以下事件(我通过简单的按钮单击完成此操作)

        var childWindow = new ChildWindow();                        
        childWindow.Closing += new EventHandler<CancelEventArgs>(OnChildWindowClosing);            
        childWindow.Show();
Run Code Online (Sandbox Code Playgroud)

现在您需要做的是走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);
    }
Run Code Online (Sandbox Code Playgroud)

HTH.