0

I have a main window view that hosts other views created as UserControls and placed in a ContentControl.

I've set:

WindowStartupLocation="CenterScreen"
SizeToContent="WidthAndHeight"

This works fine for the initial view - but when the content changes to a larger view it resizes to fit it but doesn't 're-center' the window. How do I get the main window to re-center?

jidl
  • 195
  • 1
  • 2
  • 19

1 Answers1

0

As mentioned here, try following solution

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    Rect workArea = System.Windows.SystemParameters.WorkArea;
    this.Left = (workArea.Width - this.Width) / 2 + workArea.Left;
    this.Top = (workArea.Height - this.Height) / 2 + workArea.Top;
}
Community
  • 1
  • 1
Vinkal
  • 2,964
  • 1
  • 19
  • 19