在全屏应用程序中居中模式WPF窗口

时间:2011-05-29 23:40:54

标签: wpf modal-dialog

我正在尝试创建一个始终显示在屏幕中央的模态确认对话框。

我试过用:

ConfirmCreate confirmCreate = new ConfirmCreate(); // ConfirmCreate extends Window.
bool? dialogResult = confirmCreate.ShowDialog();
confirmCreate.Owner = this;
confirmCreate.WindowStartupLocation = WindowStartupLocation.CenterOwner;

......和......

ConfirmCreate confirmCreate = new ConfirmCreate();
bool? dialogResult = confirmCreate.ShowDialog();
confirmCreate.WindowStartupLocation = WindowStartupLocation.CenterScreen;

......甚至......

ConfirmCreate confirmCreate = new ConfirmCreate(); // ConfirmCreate is a subclass of Window.
bool? dialogResult = confirmCreate.ShowDialog();
confirmCreate.WindowStartupLocation = WindowStartupLocation.Manual;
confirmCreate.Left = Width/2 - confirmCreate.Width/2;
confirmCreate.Top = Height/2 - confirmCreate.Height/2;

无济于事,有人可以解释为什么这不起作用吗?

2 个答案:

答案 0 :(得分:3)

最后使用confirmCreate.ShowDialog();尝试所有选项。换句话说,设置WindowStartupLocation的设置,然后调用ShowDialog()

答案 1 :(得分:0)

好吧,看完代码后,我意识到我有点傻。

如果我在使用ShowDialog()方法之前设置属性,则CentreScreenCentreOwner属性可以正常工作。

ConfirmCreate confirmCreate = new ConfirmCreate(); // ConfirmCreate extends Window.
confirmCreate.Owner = this;
confirmCreate.WindowStartupLocation = WindowStartupLocation.CenterOwner;
bool? dialogResult = confirmCreate.ShowDialog();

和......

ConfirmCreate confirmCreate = new ConfirmCreate();
confirmCreate.WindowStartupLocation = WindowStartupLocation.CenterScreen;
bool? dialogResult = confirmCreate.ShowDialog();