title: WPF usercontrol out of bounds of the Grid
body: <p>I have a WPF app that I'm testing which loads a XML and visualizes it in a usercontrol. Now the issue I'm having is that every time I load my user control the <code>HorizontalAlignment</code> is okay, but the <code>VerticalAlignment</code> doesn't adept to the height size of the usercontrol.</p>
  
  <p>Anyone has an idea how to solve this? </p>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>        &lt;Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:UserControls="clr-namespace:RackBuildingTesT.UserControls" x:Class="RackBuildingTesT.MainWindow"
      Title="MainWindow" Height="578" Width="758" SizeChanged="Window_SizeChanged_1"&gt;
  &lt;Grid&gt;
          &lt;DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;
          &lt;Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1"&gt;
                  &lt;Grid&gt;
                      &lt;Label Content="Welke rack laden" HorizontalAlignment="Left" VerticalAlignment="Top"/&gt;
                      &lt;ComboBox x:Name="RackChooser" HorizontalAlignment="Left" Margin="100,0,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="RackChooser_SelectionChanged"/&gt;
                  &lt;/Grid&gt;
              &lt;/Border&gt;
          &lt;Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1"&gt;
              &lt;Grid x:Name="RackGrid" Margin="0,0,0,0"&gt;
              &lt;/Grid&gt;
          &lt;/Border&gt;
          &lt;/DockPanel&gt;
      &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>MainWindow.xaml.cs</p>
  
  <pre><code>        public MainWindow()
      {
          InitializeComponent();
          LoadRackCombo();
      }
      private void LoadRackCombo()
      {
          var files = Directory.GetFiles(Properties.Settings.Default.FilePathXMLRack);
          foreach (var item in files)
          {
              RackChooser.Items.Add(item);
          }
      }
  
      private void RackChooser_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          this.RackGrid.Children.Clear();
          this.Cursor = Cursors.Wait;
          if (RackChooser.SelectedIndex == -1)
              MessageBox.Show("Select a rack");
          else
          {
              string rackFile = Convert.ToString(RackChooser.Items[RackChooser.SelectedIndex]);
              UserControls.RackBuilder r = new UserControls.RackBuilder();
              RackGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
              RackGrid.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
              r.Width = RackGrid.Width;
              r.Height = RackGrid.Height;
              var _r = TRack.CreateFromXMLFile(rackFile, null);
              r.set_Rack(_r);
              RackGrid.Children.Add(r);
          }
          this.Cursor = Cursors.Arrow;
      }
  
      private void Window_SizeChanged_1(object sender, SizeChangedEventArgs e)
      {
          RackGrid.Width = this.Width;
          RackGrid.Height = this.Height;
      }
  </code></pre>
  
  <p>RackBuilder.xaml.cs (xaml page is standard WPF usercontrol)</p>
  
  <pre><code>        public RackBuilder()
      {
          InitializeComponent();
      }
  
      public TRack fRack { get; set; }
  
      public void set_Rack(TRack value)
      {
          this.fRack = value;
          this.InvalidateVisual();
      }
  
      protected override void OnRender(DrawingContext drawingContext)
      {
          if (this.fRack != null)
          {
              var xScale = this.Width / this.fRack.Size.Width;
              var yScale = this.Height / this.fRack.Size.Height;
              var smallest = 0.0;
  
              if (xScale &lt; yScale)
                  smallest = xScale;
              else
                  smallest = yScale;
  
              foreach (var hole in this.fRack.HolePositions)
              {
                  drawingContext.DrawEllipse(Brushes.Aquamarine, null, new Point(hole.Position.X * xScale, hole.Position.Y * yScale),
                                             hole.Diameter * smallest * 0.5, hole.Diameter * smallest * 0.5);
              }
          }
      }
  </code></pre>
  
  <p>Result</p>
  
  <p><img src="https://i.stack.imgur.com/EYLMs.jpg" alt="Result of App"></p>