How do you center your main window in WPF?

前端 未结 13 1368
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 07:07

I have a WPF application and I need to know how to center the wain window programatically (not in XAML).

I need to be able to do this both at startup and in response

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 07:25

    Well, for startup time, you can set the startup location:

    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    

    Later, you'll need to query it. The information (at least for the primary screen) is available via SystemParameters.PrimaryScreenWidth/Height.

提交回复
热议问题