title: ItemsControl, VirtualizingStackPanel and ScrollViewer height
body: <p>I want to display a important list of items using an ItemsControl.</p>
  
  <p>The reason why I'm using an ItemsControl is that the DataTemplate is much more complex in the application I'm working on: The sample code provided only reflects the sizing problem I have.</p>
  
  <p>I would like :</p>
  
  <ul>
  <li>the ItemsControl to be virtualized because there is many items to display</li>
  <li><p>its size to expand to its parent container automatically (the Grid)</p>
  
  <p></p>
  
  <pre><code>&lt;Grid&gt;
      &lt;ItemsControl x:Name="My" ItemsSource="{Binding Path=Names}"&gt;
          &lt;ItemsControl.Template&gt;
              &lt;ControlTemplate&gt;
                  &lt;StackPanel&gt;
                      &lt;StackPanel&gt;
                          &lt;TextBlock Text="this is a title" FontSize="15" /&gt;
                          &lt;TextBlock Text="This is a description" /&gt;
                      &lt;/StackPanel&gt;
                      &lt;ScrollViewer CanContentScroll="True" Height="400px"&gt;
                          &lt;VirtualizingStackPanel IsItemsHost="True" /&gt;
                      &lt;/ScrollViewer&gt;
                  &lt;/StackPanel&gt;
              &lt;/ControlTemplate&gt;
          &lt;/ItemsControl.Template&gt;
          &lt;ItemsControl.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;TextBlock Text="{Binding}" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/ItemsControl.ItemTemplate&gt;
      &lt;/ItemsControl&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p></li>
  </ul>
  
  <p>The code behind is :</p>
  
  <pre><code>public partial class Page1: Page
  {
      public List&lt;string&gt; Names { get; set; }
      public Page1()
      {
          InitializeComponent();
  
          Names = new List&lt;string&gt;();
          for(int i = 0; i &lt; 10000; i++)
              Names.Add("Name : " + i);
  
          My.DataContext = this;
      }
  }
  </code></pre>
  
  <p>As I force the ScrollViewer height to 400px, ItemsControl virtualization works as I expect: The ItemsControl displays the list very quickly, regardless of how many items it contains.</p>
  
  <p>However, if I remove Height="400px", the list will expand its height to display the whole list, regardless its parent container height. Worse: it appears <em>behind</em> its container.</p>
  
  <p>Putting a scrollviewer around the ItemsControl gives the expected visual result, but the virtualization goes away and the list takes too much time to display.</p>
  
  <p>How can I achieve both automatic height expansion and virtualization of my ItemsControl ?</p>