title: WPF Resize UserControl with aspect ratio
body: <p>I have a UserControl and that UserControl has to be resized with aspect ratio. 
  That means: width:height = 2:1.
  Currently I am using this code:</p>
  
  <pre><code>    protected override Size ArrangeOverride(Size arrangeBounds)
      {
          if (ActualWidth == 0 || ActualHeight == 0) return arrangeBounds;
          base.ArrangeOverride(arrangeBounds);
          double ratio = 2;
  
          if (Parent != null)
          {
              var size = new Size(arrangeBounds.Height * ratio, arrangeBounds.Height);
  
              double containerWidth = ((FrameworkElement)Parent).ActualWidth;
              if (containerWidth &lt; size.Width)
              {
                  double newHeight = arrangeBounds.Height * (containerWidth / size.Width);
                  canvas.Width = newHeight * ratio;
                  canvas.Height = newHeight;
              }
              else
              {
                  canvas.Width = size.Height * ratio;
                  canvas.Height = size.Height;
              }
          }
  
          return arrangeBounds;
      }
  </code></pre>
  
  <p>But it is not really working. That means it works but not every time. If I max. the window it sometimes does not get resized, so its a bit "random" if the control gets resized. So if someone would have a better solution if would be very nice.</p>