Created window. UserControl is loaded into it by default, when I click one button to change one control to another, it is not displayed in the center. Although the window mapping property is set in the center.

The question is how to dynamically change UserControls to display the window again in the center? The size of the window is adjusted to the contents of the UserControl Now the behavior is this: when you first start everything is fine, as soon as I press the registration button, the window simply expands and takes shape not in the center.

  • without pictures and code is hard to say. Yes, and that for UserControl is also not clear. If you create a multi-window application, then try creating a new window with this parameter. WindowStartupLocation = "CenterOwner" - Alexandr
  • The general window, in it only UserControl in which there is a content change. I solved the problem, subscribed to the SizeChanged event and redefined the window in it. Yes, and the meaning in the pictures, like all the details painted. About the new window? I do not welcome this style. I prefer to do everything in one window while it is possible. :) - Maxim
  • Show your code, without it, how do we know where you have a mistake? - VladD
  • Yes, there is no error in the code. It was just necessary to recalculate the position of the window. - Max

1 answer 1

private void OnSizeChanged(object sender, SizeChangedEventArgs e) { double screenHeight = SystemParameters.FullPrimaryScreenHeight; double screenWidth = SystemParameters.FullPrimaryScreenWidth; this.Top = (screenHeight - this.ActualHeight) / 2; this.Left = (screenWidth - this.ActualWidth) / 2; } 

By subscribing to this event, I calculated the window position again.

  • SystemParameters.FullPrimaryScreenHeight probably not quite right - because the window is not necessarily on the primary screen? - VladD
  • Apparently, you need to look here . - VladD