3

I want to change the page width and height in page loaded, When I change the size the page size change but the windows does not appear in center screen, Even when I but in xaml code :

WindowStartupLocation="CenterScreen"

The code I used in code behind in window load is:

 this.Width = 1366;
 this.Height = 768;

This Answer provide a method to center window Programmatically, But I wonder why the following do not work :

this.WindowStartupLocation = WindowStartupLocation.CenterScreen;

Any suggest Thanks

Abdulsalam Elsharif
  • 4,773
  • 7
  • 32
  • 66
  • Set the Width and Height attributes in the XAML. When you set `WindowStartupLocation`, you should expect that to set the location of the window at startup. You should not expect it to keep updating the window location forever unless the property is called `KeepUpdatingWindowLocationForever`. If you really want code to do that, [here's some code to do that](http://stackoverflow.com/a/4022379/424129). – 15ee8f99-57ff-4f92-890c-b56153 Feb 23 '17 at 19:38
  • I want to change the size according to some conditions, that is why I put it in code behind – Abdulsalam Elsharif Feb 23 '17 at 19:42
  • You could try binding. Or you could just copy and paste [the window centering method from the answer I linked](http://stackoverflow.com/a/4022379/424129) and call that every time you change width and height. – 15ee8f99-57ff-4f92-890c-b56153 Feb 23 '17 at 19:43
  • 1
    Possible duplicate of [How do you center your main window in WPF?](http://stackoverflow.com/questions/4019831/how-do-you-center-your-main-window-in-wpf) – 15ee8f99-57ff-4f92-890c-b56153 Feb 23 '17 at 19:43

0 Answers0