如何将来自.ShowDialog();
的对话框的位置设置为显示在mainWindows的中心。
这是我尝试设置位置的方式。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
PresentationSource source = PresentationSource.FromVisual(this);
if (source != null)
{
Left = ??
Top = ??
}
}
发布于 2010-11-30 05:53:03
您可以尝试在已加载的事件中获取MainWindow,如下所示
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;
}
发布于 2011-06-30 14:27:00
在属于该对话框的XAML中:
<Window ... WindowStartupLocation="CenterOwner">
在C#中,当您实例化对话框时:
MyDlg dlg = new MyDlg();
dlg.Owner = this;
if (dlg.ShowDialog() == true)
{
...
发布于 2010-11-30 02:12:08
我认为使用xaml标记更容易
<Window WindowStartupLocation="CenterOwner">
https://stackoverflow.com/questions/4306593
复制相似问题