I have a WPF window that has a MaxWidth set, so when I hit the Maximize button, it maximizes vertically but not horizontally. This is expected behavior. However, the window always docks to the left side of the screen (Windows 7, if that matters) and I want it to be centered horizontally when Maximized. I tried adding the following StateChanged
event handler, but it doesn't seem to do anything:
private void wdw_mainWindow_StateChanged(object sender, EventArgs e)
{
switch (WindowState)
{
case WindowState.Maximized:
var windowWidth = (double)GetValue(WidthProperty);
Left = (SystemParameters.PrimaryScreenWidth / 2) - (windowWidth / 2);
break;
}
}
I set a breakpoint on the switch
statement and this code definitely gets hit when I hit the Maximize button in my app. However, even after Left
gets set, the window remains firmly stuck to the left side of the screen. What's going on?