title: How can you make a WPF Expander stretch vertically within a vertical StackPanel
body: <p>Given the XAML below, is it possible to make an adjustment so that when either of the <code>Expander</code> items are expanded, the <code>TextBlock</code> content stretches vertically to fill the remaining space? (Note that this XAML is purposely simplified, the solution must satisfy this simple example and any data bound <code>ItemsSource</code> scenario).</p>
  
  <p>The <code>StackPanel</code> itself stretches to fill the space (since it is contained within a <code>Grid</code>), so I assume there is a conflict within the <code>StackPanel</code> <a href="http://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Controls/Stack.cs,9ef706576207e5a6,references" rel="nofollow noreferrer">measuring code</a> that does not allow infinitely large content elements. I'm not even sure if this is possible to resolve elegantly, it may just require manually overriding the measuring code and determining the optimal expanded height for the content container.</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;StackPanel&gt;
          &lt;Expander Header="Item 1"&gt;
              &lt;TextBlock Background="LightCoral" Text="Content 1 should be stretched vertically when expanded"/&gt;
          &lt;/Expander&gt;
          &lt;Expander Header="Item 2"&gt;
              &lt;TextBlock Background="LightBlue" Text="Content 2 should be stretched vertically when expanded"/&gt;
          &lt;/Expander&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>NOTE</strong>: I read through <a href="https://stackoverflow.com/questions/569095/how-to-get-stackpanels-children-to-fill-maximum-space-downward">How to get StackPanel&#39;s children to fill maximum space downward?</a> and it is not really related to this question as the solution to that post is to use a <code>DockPanel</code> which is not possible in a data bound <code>ItemsSource</code> scenario.</p>