title: Having trouble getting contents of ListBox to resize with it
body: <p>I tried the suggestion <a href="https://stackoverflow.com/questions/2323063/wpf-resizing-listbox-contents-according-to-listbox-dimensions">here</a> regarding Stretching, but it didn't work for me.</p>
  
  <p>I have a TabItem that hosts a UserControl that is essentially just a ListBox with an ItemsPanel.  The ItemsPanel forces the ListBox to display its contents horizontally.  The contents of the ListBox are databound to an <code>ObservableCollection&lt;StackPanelContents&gt;</code>, which is another UserControl.  <code>StackPanelContents</code> itself basically just contains a <code>List</code> of objects with some visual representation (they contain a UIElement for visualization).</p>
  
  <p>In general the code works properly in that it renders all of the data that I have contained in the <code>ObservableCollection</code>, but the problem is that the items don't resize as the window is enlarged or shrunk.</p>
  
  <p>Here's an overview of what the class hierarchy looks like:</p>
  
  <p><img src="https://farm5.static.flickr.com/4136/4860039669_3c70061cf9.jpg" alt="alt text"></p>
  
  <p>And here are the results:</p>
  
  <p><img src="https://farm5.static.flickr.com/4142/4860034741_e7fce721f6_m.jpg" alt="alt text"></p>
  
  <p>The XAML for the main window just has the TabControl and TabItem:</p>
  
  <pre><code>&lt;Window x:Class="ResizeStackPanelInListBox.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300"&gt;
  
      &lt;TabControl&gt;
          &lt;TabItem Header="Test" x:Name="MyTabItem"&gt;
              &lt;!-- contents are set in code behind --&gt;
          &lt;/TabItem&gt;
      &lt;/TabControl&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The ListBoxContainer XAML that displays the StackPanelContents looks like this:</p>
  
  <pre><code>&lt;UserControl x:Class="ResizeStackPanelInListBox.ListBoxContainer"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Height="Auto" Width="Auto"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
              &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;TextBlock&gt;ListBox with StackPanels as its ListBoxItems:&lt;/TextBlock&gt;
          &lt;ListBox Grid.Row="1" ItemsSource="{Binding StackPanels}" HorizontalContentAlignment="Stretch"&gt;
              &lt;ListBox.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal" Width="Auto" Height="Auto" /&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ListBox.ItemsPanel&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>And the StackPanelContents UserControl looks like this:</p>
  
  <pre><code>&lt;UserControl x:Class="ResizeStackPanelInListBox.StackPanelContents"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Height="Auto" Width="Auto"&gt;
  
      &lt;StackPanel&gt;
          &lt;TextBlock Text="StackPanel"&gt;&lt;/TextBlock&gt;
          &lt;StackPanel x:Name="MyStackPanel"&gt;
          &lt;/StackPanel&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Can anyone explain why this isn't resizing automatically, and how I can go about resolving this?  Am I going to have to write my own custom panel to deal with this, and use <code>MeasureOverride</code> and <code>Arrange</code>?</p>