开发者

How to know the position of the Silverlight ChildWindow when you close it

Please, help me.

public myChildWindow()
{
    InitializeComponent();

    // set left and top from saved values
    Margin = new Thickness(70, 50, 0, 开发者_如何学C0);
}

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


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜