title: How do I keep aspect ratio on scalable, scrollable content in WPF?
body: <p>I'm fairly new to WPF and I've come across a problem that seems a bit tricky to solve.  Basically I want a 4x4 grid thats scalable but keeps a square (or any other arbitrary) aspect ratio.  This actually seems quite tricky to do, which surprises me because I would imagine its a reasonably common requirement.</p>
  
  <p>I start with a Grid definition like this:</p>
  
  <pre><code>&lt;Grid&gt;
    &lt;Grid.RowDefinitions&gt;
      &lt;Grid.RowDefinition Height="*"/&gt;
      &lt;Grid.RowDefinition Height="*"/&gt;
      &lt;Grid.RowDefinition Height="*"/&gt;
      &lt;Grid.RowDefinition Height="*"/&gt;
    &lt;/Grid.RowDefinitions&gt;
    &lt;Grid.ColumnDefinitions&gt;
      &lt;Grid.ColumnDefinition Width="*"/&gt;
      &lt;Grid.ColumnDefinition Width="*"/&gt;
      &lt;Grid.ColumnDefinition Width="*"/&gt;
      &lt;Grid.ColumnDefinition Width="*"/&gt;
    &lt;/Grid.ColumnDefinition&gt;
    ...
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Now if you set that to stretch, it can fill the Window or whatever container you put it in.  The rows and column are uniform but the aspect ratio isn't fixed.</p>
  
  <p>Then I tried putting it in a StackPanel to use the available space.  Didn't help.  What did get me most of the way there was when I remembered Viewboxes.</p>
  
  <pre><code>&lt;StackPanel Orientation="Horizontal"&gt;
    &lt;Viewbox&gt;
      &lt;Grid Height="1000" Width="1000"&gt; &lt;!-- this locks aspect ratio --&gt;
        &lt;Grid.RowDefinitions&gt;
          &lt;Grid.RowDefinition Height="*"/&gt;
          &lt;Grid.RowDefinition Height="*"/&gt;
          &lt;Grid.RowDefinition Height="*"/&gt;
          &lt;Grid.RowDefinition Height="*"/&gt;
        &lt;/Grid.RowDefinitions&gt;
        &lt;Grid.ColumnDefinitions&gt;
          &lt;Grid.ColumnDefinition Width="*"/&gt;
          &lt;Grid.ColumnDefinition Width="*"/&gt;
          &lt;Grid.ColumnDefinition Width="*"/&gt;
          &lt;Grid.ColumnDefinition Width="*"/&gt;
        &lt;/Grid.ColumnDefinition&gt;
        ...
      &lt;/Grid&gt;
    &lt;/viewbox&gt;
    &lt;Label HorizontalAlignment="Stretch"&gt;Extra Space&lt;/Label&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>Now my content scales and keeps aspect ratio.  The problem is that if the window isn't wide enough some of my grid is off the screen.  I'd like to be able to scroll to it if that were the case.  Likewise, I might need a minimum size, which might lead to vertical scrolling too.</p>
  
  <p>Now I've tried putting my StackPanel and Grid (separately) in an appropriate ScrollViewer container but then the content no longer scales to fit the window.  It goes to full size, which is no good.</p>
  
  <p>So how do I go about doing this?  Am I barking up the wrong tree?  Is there a better/easier way of doing this?</p>