title: How to recenter UserControl(s) in Window
body: <p>We have BaseDialogView with next xaml code:</p>
  
  <pre><code>&lt;Window x:Class="Test.BaseDialogView"       
      Height="475"       
      WindowStartupLocation="CenterOwner"       
      SizeToContent="Height"
      ResizeMode="CanResize"
      SizeChanged="Window_SizeChanged"&gt;
    &lt;ContentControl Content="{Binding ContentPage}" /&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>BaseDialogViewModel  class:</p>
  
  <pre><code>public class BaseDialogViewModel : AbstractNotifyPropertyChangedItem
  {
      private UserControl contentPage;
      public UserControl ContentPage
      {
          get { return this.contentPage; }
          set
          {
              if (this.contentPage != value)
              {
                  this.contentPage = value;
                  this.RaisePropertyChanged(() =&gt; this.ContentPage);
              }
          }
      }
  }
  </code></pre>
  
  <p>The usage is very simple:</p>
  
  <pre><code>BaseDialog dialog = new BaseDialog();
  BaseDialogViewModel dialogVm = new BaseDialogViewModel();
  
  dialog.Owner = Application.Current.MainWindow;
  dialog.DataContext = dialogVm ;
  
  dialogVm.ContentPage = new ActivationView();
  dialogVm.ContentPage.DataContext = new ActivationViewModel();            
  </code></pre>
  
  <p>So basically once you have an instance of <code>BaseDialog</code>, you just set ContentControl (by setting <code>dialog.ContentPage</code> and <code>dialog.ContentPage.DataContext</code>). </p>
  
  <p>ActivationView is very simple. For example:</p>
  
  <pre><code> &lt;UserControl x:Class="Test.ActivationView" d:DesignHeight="400" d:DesignWidth="700" MaxWidth="700"&gt;
      &lt;Grid&gt; .... what ever you need
   &lt;/UserControl&gt;
  </code></pre>
  
  <p>The problem is that different <code>UserControls</code> windows are set, which have different width and height. When the first UserControl is shown it's place in the center of the MainWindow, which is ok. Then each new userControl is shown, but it's not centered. <strong>How do I center the BaseDialog window for each usercontrol?</strong></p>
  
  <p>I tried this (BaseDialogView):</p>
  
  <pre><code>private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  {
    Window w = sender as Window;
    this.Top = (Application.Current.MainWindow.Height - w.ActualHeight) / 2;
  }
  </code></pre>
  
  <p>but does not work ok (Some usercontrols are still not pixel centered). I also tried adding this to BaseDialogView Xaml</p>
  
  <pre><code>&lt;Window .... VerticalAlignment="Center"&gt;
  </code></pre>
  
  <p>but it seems to be working only for initial instance.</p>