How to center a WPF app on screen?

前端 未结 8 824
清歌不尽
清歌不尽 2021-01-30 09:53

I want to center my WPF app on startup on the primary screen. I know I have to set myWindow.Left and myWindow.Top, but where do I get the values?

I found System.Wi

相关标签:
8条回答
  • 2021-01-30 10:38
    var window = new MyWindow();
    

    for center of the screen use:

    window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
    

    for center of the parent window use:

    window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
    
    0 讨论(0)
  • 2021-01-30 10:43

    I prefer to put it in the WPF code.

    In [WindowName].xaml file:

    <Window x:Class=...
    ...
    WindowStartupLocation ="CenterScreen">
    
    0 讨论(0)
提交回复
热议问题