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

发布于 2024-08-30 16:42:19 字数 360 浏览 9 评论 0原文

请帮我。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2024-09-06 16:42:19

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

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

        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。

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.

汐鸠 2024-09-06 16:42:19

如果您订阅了 Closing 事件而不是 Closed 事件,您可以从窗口中找到 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文