WPF:如何设置对话框位置显示在应用程序的中心? [英] WPF : How to set a Dialog position to show at the center of the application?

查看:220
本文介绍了WPF:如何设置对话框位置显示在应用程序的中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置来自 .ShowDialog(); 的对话框的位置显示在mainWindows的中心。

How to set Dialog's position that came from .ShowDialog(); to show at the center of the mainWindows.

这是我尝试设置位置的方式。

This is the way I try to set position.

private void Window_Loaded(object sender, RoutedEventArgs e)
{        
    PresentationSource source = PresentationSource.FromVisual(this);
    if (source != null)
    {
        Left = ??
        Top = ??
    }
}


推荐答案

你可以尝试在Loaded事件中保持MainWindow,如下所示

You can try to get a hold of the MainWindow in the Loaded event like this

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Application curApp = Application.Current;
    Window mainWindow = curApp.MainWindow;
    this.Left = mainWindow.Left + (mainWindow.Width - this.ActualWidth) / 2;
    this.Top = mainWindow.Top + (mainWindow.Height - this.ActualHeight) / 2;
}

这篇关于WPF:如何设置对话框位置显示在应用程序的中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆