我有一个子窗口,高度=300,宽度=400。当我最大化这个子窗口时,它会显示在left=0和top=0的位置。我想让它显示在主窗口的中心
我已经尝试了以下方法
-
在Window_StateChanged事件,我试图改变其位置为左=100和Top=140。值被赋值,但仍然显示在left=0和Top=0位置
-
我也试图改变它的位置在Window_SizeChanged事件,但它也没有工作
-
然后我觉得布局可能不会刷新,所以我使用this.UpdateLayout()刷新了它,但不起作用。
这可能是你想要的
void Window1_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == System.Windows.WindowState.Maximized)
{
this.WindowState = System.Windows.WindowState.Normal;
this.Left = (App.Current.MainWindow.Width - this.MaxWidth) / 2 + App.Current.MainWindow.Left;
this.Top = (App.Current.MainWindow.Height - this.MaxHeight) / 2 + App.Current.MainWindow.Top;
this.Width = this.MaxWidth;
this.Height = this.MaxHeight;
}
}