开发者

Centring a modal WPF window, in a full-screen app

开发者 https://www.devze.com 2023-03-09 07:18 出处:网络
I\'m attempting to create a modal confirmation dialog that always appears in the centre of the screen.

I'm attempting to create a modal confirmation dialog that always appears in the centre of the screen.

I've tried to use:

ConfirmCreate confirmCreate = new ConfirmCreate(); // ConfirmCreate extends Window.
b开发者_如何学编程ool? dialogResult = confirmCreate.ShowDialog();
confirmCreate.Owner = this;
confirmCreate.WindowStartupLocation = WindowStartupLocation.CenterOwner;

... and...

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

... and even ...

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;

To no avail, can someone explain why this isn't working?


Try all your options with confirmCreate.ShowDialog(); at the end. In other words, set the settings for WindowStartupLocation and then call ShowDialog()


Well, after looking at the code, I realised I was being a bit stupid.

If I set the properties before using the ShowDialog() method, the CentreScreen and CentreOwner properties do their job properly.

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

and ...

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号