title: Center popup in XAML
body: <p>I have created a Popup by using the following code, but I can not figure out how to center it <br />
  I tried to automatically change the margin on runtime, but I could not figure out how to do it, but do anybody has an idea of how to center the popup? <br />
  It does not has a standard-dimension cause I need to globalize my program</p>
  
  <pre class="lang-xaml prettyprint-override"><code>&lt;Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="MainGrid"&gt;
     &lt;Popup x:Uid="LoginPopup" IsOpen="True" Name="LoginPopup"&gt;
        &lt;Grid&gt;
           &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="Auto" /&gt;
           &lt;/Grid.RowDefinitions&gt;
           &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto" /&gt;
              &lt;ColumnDefinition Width="*" /&gt;
           &lt;/Grid.ColumnDefinitions&gt;
           &lt;TextBlock Margin="10" Grid.Column="0" Grid.Row="0" Text="App Name" Grid.ColumnSpan="2" Style="{StaticResource HeaderTextStyle}" /&gt;
           &lt;TextBlock Margin="10" Grid.Column="0" Grid.Row="1" Text="Username" Style="{StaticResource ResourceKey=SubheaderTextStyle}" /&gt;
           &lt;TextBox Margin="10" Grid.Column="1" Grid.Row="1" Name="InputUsername" /&gt;
           &lt;TextBlock Margin="10" Grid.Column="0" Grid.Row="2" Text="Password" Style="{StaticResource ResourceKey=SubheaderTextStyle}" /&gt;
           &lt;PasswordBox Margin="10" Grid.Column="1" Grid.Row="2" Name="InputPassword" /&gt;
           &lt;StackPanel  Margin="10" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left" Orientation="Horizontal"&gt;
              &lt;Button Name="Login"  x:Uid="LoginPopupLogin"  /&gt;
              &lt;Button Name="Cancel" x:Uid="LoginPopupCancel" /&gt;
           &lt;/StackPanel&gt;
        &lt;/Grid&gt;
     &lt;/Popup&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried with user1603313's answer below, but it did not do the trick, as it says the size of the grid inside the popup is NaN. <br />
  I also tried to move the method into the grid, but it didn't do the trick either<br />
  The method I am talking about is this with the grid updated properly</p>
  
  <pre class="lang-cs prettyprint-override"><code>private void LoginPopup_Loaded_1(object sender, RoutedEventArgs e)
  {
     LoginPopup.HorizontalOffset = (Window.Current.Bounds.Width - gdChild.ActualWidth) / 2;
     LoginPopup.VerticalOffset = (Window.Current.Bounds.Height - gdChild.ActualHeight) / 2;
  }
  </code></pre>