views:

413

answers:

2

Please, help me.

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
    ...
}
A: 

You can find out the Left/Top values from the Window provided you are subscribing to the Closing event and not Closed

ie:

private void Button_Click(object sender, RoutedEventArgs e)
{
    LoginWindow loginWnd = new LoginWindow();
    loginWnd.Closing += new EventHandler(loginWnd_Closing);
}

Then to get the position values use:

double x = GetValue(ChildWindow.LeftProperty) as double;
double y = GetValue(ChildWindow.TopProperty) as double;
Scott Barnes
This is not work (. LeftProperty, TopProperty - compiler error.And var x = myChildWindow.GetValue(MarginProperty);return {0,0,0,0}
Silver
A: 

Oops you are right, try this:

Wire up the window to the following events (I did this via simple button click)

        var childWindow = new ChildWindow();                        
        childWindow.Closing += new EventHandler<CancelEventArgs>(OnChildWindowClosing);            
        childWindow.Show();

Now what you need to do is walk the ChildWindow PARTS DOM and find the ContentRoot which will give you the position.

    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.

Scott Barnes
Ok. It is necessary to analyze the chrome. Thank you.
Silver