在 mainwindow 中居中新 window
Centering new window in mainwindow
这可能是重复的,但我用谷歌搜索并查看了 Whosebug 以尝试找到解决此问题的方法,但它仍然无法正常工作。
我正在尝试将 window(输入对话框)置于主 window 的中心。
它应该垂直居中,新 window(对话框)的宽度必须是 MainWindow.
的宽度
这是我目前尝试的方法:
Application curApp = Application.Current;
Window mainWindows = curApp.MainWindow;
this.Left = mainWindows.Left;
this.Top = mainWindows.Top + (mainWindows.ActualHeight - this.ActualHeight) / 2;
this.Width = mainWindows.ActualWidth;
以上代码位于Dialog的loaded事件中Window。
虽然这将它垂直居中,但它并没有将它放在主Window的确切中心(即 Left 属性不匹配。)
请帮忙。
我是这样调用对话框的:
var dialog = new MyDialog();
dialog.ShowMessage("Capture Amount", "Please enter the 'Capture Amount'");
if (dialog.ShowDialog() == true) {
DialogResponse = dialog.ResponseAmount.ToString();
CustomTitleMain = "Main Window: " + CurrencyCode.ToString() + " " + DialogResponse.ToString();
}
DialogResponse、CurrencyCode 和 ResponseAmount 是属性。前两个是字符串,最后一个是小数。
使用 System.Windows.WindowStartupLocation.CenterOwner
并设置对话框 Owner
属性 window。
Window subWindow = new MyWindow();
subWindow.Owner = Application.Current.MainWindow;
subWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
subwindow.ShowDialog();
您可以在 Init 中设置对话框的 StartupLocation,例如:
Application curApp = Application.Current;
Window mainWindows = curApp.MainWindow;
this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
this.Owner = mainWindows;
this.Left = mainWindows.Left;
this.Top = mainWindows.Top + (mainWindows.ActualHeight - this.ActualHeight) / 2;
this.Width = mainWindows.ActualWidth;
或者调用时设置:
var dialog = new MyDialog();
dialog.Owner = Application.Current.MainWindow;
dialog.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
dialog.ShowMessage("Capture Amount", "Please enter the 'Capture Amount'");
if (dialog.ShowDialog() == true) {
DialogResponse = dialog.ResponseAmount.ToString();
CustomTitleMain = "Main Window: " + CurrencyCode.ToString() + " " + DialogResponse.ToString();
}
但请记住,您也可以通过 xaml 设置所有这些参数。在我看来,这使它更具可读性。
这可能是重复的,但我用谷歌搜索并查看了 Whosebug 以尝试找到解决此问题的方法,但它仍然无法正常工作。 我正在尝试将 window(输入对话框)置于主 window 的中心。 它应该垂直居中,新 window(对话框)的宽度必须是 MainWindow.
的宽度这是我目前尝试的方法:
Application curApp = Application.Current;
Window mainWindows = curApp.MainWindow;
this.Left = mainWindows.Left;
this.Top = mainWindows.Top + (mainWindows.ActualHeight - this.ActualHeight) / 2;
this.Width = mainWindows.ActualWidth;
以上代码位于Dialog的loaded事件中Window。
虽然这将它垂直居中,但它并没有将它放在主Window的确切中心(即 Left 属性不匹配。)
请帮忙。
我是这样调用对话框的:
var dialog = new MyDialog();
dialog.ShowMessage("Capture Amount", "Please enter the 'Capture Amount'");
if (dialog.ShowDialog() == true) {
DialogResponse = dialog.ResponseAmount.ToString();
CustomTitleMain = "Main Window: " + CurrencyCode.ToString() + " " + DialogResponse.ToString();
}
DialogResponse、CurrencyCode 和 ResponseAmount 是属性。前两个是字符串,最后一个是小数。
使用 System.Windows.WindowStartupLocation.CenterOwner
并设置对话框 Owner
属性 window。
Window subWindow = new MyWindow();
subWindow.Owner = Application.Current.MainWindow;
subWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
subwindow.ShowDialog();
您可以在 Init 中设置对话框的 StartupLocation,例如:
Application curApp = Application.Current;
Window mainWindows = curApp.MainWindow;
this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
this.Owner = mainWindows;
this.Left = mainWindows.Left;
this.Top = mainWindows.Top + (mainWindows.ActualHeight - this.ActualHeight) / 2;
this.Width = mainWindows.ActualWidth;
或者调用时设置:
var dialog = new MyDialog();
dialog.Owner = Application.Current.MainWindow;
dialog.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
dialog.ShowMessage("Capture Amount", "Please enter the 'Capture Amount'");
if (dialog.ShowDialog() == true) {
DialogResponse = dialog.ResponseAmount.ToString();
CustomTitleMain = "Main Window: " + CurrencyCode.ToString() + " " + DialogResponse.ToString();
}
但请记住,您也可以通过 xaml 设置所有这些参数。在我看来,这使它更具可读性。