title: Stretching controls to fill ItemsControl
body: <p>I am attempting to write a simple WPF learning project which creates a set of buttons inside a resizeable main window.  There is to be one <code>Button</code> per entry in a collection, and the contents of that collection may vary during run-time.  I want the buttons to fill the entire window (e.g. 1 button @ 100% width, 2 buttons @ 50% width, 3 buttons @ 33% width, etc. all at 100% height).  A simplified version of what I've written so far is:</p>
  
  <pre><code>&lt;ItemsControl x:Name="itemscontrolButtons" ItemsSource="{Binding}"&gt;
    &lt;ItemsControl.ItemTemplate&gt;
      &lt;DataTemplate&gt;
        &lt;Button Tag="{Binding}"&gt;
          &lt;TextBlock Text="{Binding}" /&gt;
        &lt;/Button&gt;
      &lt;/DataTemplate&gt;
    &lt;/ItemsControl.ItemTemplate&gt;
    &lt;ItemsControl.ItemsPanel&gt;
      &lt;ItemsPanelTemplate&gt;
        &lt;StackPanel Orientation="Horizontal" /&gt;
      &lt;/ItemsPanelTemplate&gt;
    &lt;/ItemsControl.ItemsPanel&gt;
  &lt;/ItemsControl&gt;
  ...    
  List&lt;string&gt; listButtonTexts = new List&lt;string&gt;() { "Button1", "Button2" };
  ...
  itemscontrolButtons.DataContext = listButtonTexts;
  </code></pre>
  
  <p>This results in this:</p>
  
  <p><img src="https://i.stack.imgur.com/SrnjF.jpg" alt="alt text"></p>
  
  <p>I have been unable to make the buttons stretch to fit the width and my attempts to use a <code>Grid</code> instead of <code>StackPanel</code> were fruitless.</p>
  
  <p>Then, as an optional improvement, I would appreciate suggestions on how to adjust it such that if there are so many buttons that they cannot fit properly on a line or are narrower than a threshold, it will wrap onto a new line (thereby halving the button heights if going from 1 to 2 rows).</p>
  
  <p>I'd like to emphasize that I'd like to know how to do this the <em>WPF way</em>.  I realize I can consume window resize messages and resize the controls explicitly, and that's how I'd have done it with MFC or WinForms, but from what I've read that is not how such things should be done with WPF.</p>