0

I try to maximise/restore a window through a button click. If I start my application and click the maximise button, the window moves to the top/left of the monitor it's on but does not resize to the full width/height. If I restore the window and then manually resize it by dragging a border and click the maximise button again, it maximises properly. What does the manual resize do behind the scenes that make the maximise work?

I have tried to force a resize to set the Width/Height of the Window before maximising, even calling Measure with the new size but to no avail.

The Window has the following attributes in XAML (problem also occurs with resize mode set to CanResize):

SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
ResizeMode="CanResizeWithGrip"

The code to maximise/restore is simply (where Window is a Window object):

   if(Window.WindowState == WindowState.Maximized)
      Window.WindowState = WindowState.Normal;
   else
      Window.WindowState = WindowState.Maximized;

The expected behaviour is that the window maximises properly regardless if the user resized it manually first.

Daap
  • 345
  • 4
  • 10
  • Can you try SystemCommands.RestoreWindow(this); – kiran Nov 09 '19 at 09:23
  • https://stackoverflow.com/questions/5531548/how-to-restore-a-minimized-window-in-code-behind/31625901#31625901 - Can you please check this question. – kiran Nov 09 '19 at 09:27
  • It turns out that SizeToContent combined with Maximized (see this link: https://stackoverflow.com/questions/25591359/windowstate-and-sizetocontent-do-not-work-simultaneously) does not work correctly. Although I still don't understand why it does work after manually resizing the window first. – Daap Nov 10 '19 at 00:14
  • 1
    @kiran Surprisingly, that works (although the maximise was what I was looking for, so I used SystemCommands.MaximizeWindow). Apparently that method also does something in the background that makes it work. Thanks! – Daap Nov 10 '19 at 00:20

0 Answers0