title: Virtualize ItemsControl with a height of * inside an ItemsControl
body: <p>I have ran into a virtualization problem recently and I have narrowed it down to the following code.</p>
  
  <p>The reason why virtualization doesn't work in the following snippet is due to the fact that the child has no specific height. So my guess is that it expands forever and virtualization breaks.</p>
  
  <p>Giving the child a specific height fixes the problem but then the interface becomes two unsightly scroll bars when I would like one scroll bar, to scroll through the whole content generated by the items controls (if its child, or not).</p>
  
  <p>My question is, is this possible? if so how can I achieve this? Somehow the child needs to calculate the size of itself without breaking virtualization. It seems setting a height of * does not work.</p>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="WpfItemsControlVirtualization.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="500" &gt;
  &lt;Window.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;!--Virtualised ItemsControl--&gt;
          &lt;Style x:Key="ItemsControlVirtialisedStyle" TargetType="ItemsControl"&gt;
              &lt;Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/&gt;
              &lt;Setter Property="ScrollViewer.CanContentScroll" Value="True"/&gt;
              &lt;Setter Property="ItemsPanel"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ItemsPanelTemplate&gt;
                          &lt;VirtualizingStackPanel /&gt;
                      &lt;/ItemsPanelTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="ItemsControl"&gt;
                          &lt;ScrollViewer Padding="{TemplateBinding Control.Padding}" Focusable="False"&gt;
                              &lt;ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /&gt;
                          &lt;/ScrollViewer&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Window.Resources&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;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Button Grid.Row="0" Content="Go" Click="ButtonBase_OnClick"/&gt;
      &lt;Button Grid.Row="1" Content="Expand" Click="ButtonBase_OnClick2"/&gt;
      &lt;Expander Grid.Row="2" &gt;
          &lt;ItemsControl ItemsSource="{Binding Collection}" Style="{StaticResource ItemsControlVirtialisedStyle}" VirtualizingPanel.ScrollUnit="Pixel"&gt;
              &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Grid&gt;
                          &lt;Grid.RowDefinitions&gt;
                              &lt;!-- &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt; --&gt; &lt;!-- VIRTUALIZATION BREAK --&gt;
                              &lt;RowDefinition Height="500"&gt;&lt;/RowDefinition&gt;
                          &lt;/Grid.RowDefinitions&gt;
                          &lt;ItemsControl ItemsSource="{Binding Collection}" Style="{StaticResource ItemsControlVirtialisedStyle}" VirtualizingPanel.ScrollUnit="Pixel"&gt;
                                  &lt;ItemsControl.ItemTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;TextBox Text="{Binding Test}" /&gt;
                                      &lt;/DataTemplate&gt;
                                  &lt;/ItemsControl.ItemTemplate&gt;
                              &lt;/ItemsControl&gt;
                      &lt;/Grid&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.ItemTemplate&gt;
          &lt;/ItemsControl&gt;
      &lt;/Expander&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>MainWindow.xaml.cs</p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.Runtime.CompilerServices;
  using System.Windows;
  
  namespace WpfItemsControlVirtualization
  {
      /// &lt;summary&gt;
      /// Implements the INotifyPropertyChanged interface for data binding purposes. 
      /// &lt;/summary&gt;
      public abstract class ViewModelBase : INotifyPropertyChanged, INotifyPropertyChanging
      {
          #region Abstract
  
          public void AlertPropertyChanging(string propertyName)
          {
              OnPropertyChanging(propertyName);
          }
  
          public void AlertPropertyChanged(string propertyName)
          {
              OnPropertyChanged(propertyName);
          }
  
          protected void OnPropertyChanged(string propertyName)
          {
              var handler = PropertyChanged;
              if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
          }
  
          protected void OnPropertyChanging(string propertyName)
          {
              var handler = PropertyChanging;
              if (handler != null) handler(this, new PropertyChangingEventArgs(propertyName));
          }
  
          protected bool Set&lt;T&gt;(ref T field, T value, [CallerMemberName] string propertyName = null)
          {
              if (EqualityComparer&lt;T&gt;.Default.Equals(field, value)) return false;
              OnPropertyChanging(propertyName);
              field = value;
              OnPropertyChanged(propertyName);
              return true;
          }
  
          protected void Set(Action action, string propertyName = null)
          {
              OnPropertyChanging(propertyName);
              if (action != null) action();
              OnPropertyChanged(propertyName);
          }
  
          #endregion
  
          #region Implementation of INotifyPropertyChanged
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          #endregion Implementation of INotifyPropertyChanged
  
          #region Implementation of INotifyPropertyChanging
  
          public event PropertyChangingEventHandler PropertyChanging;
  
          #endregion Implementation of INotifyPropertyChanging
      }
  
      public class MySubDataTest : ViewModelBase
      {
          public MySubDataTest()
          {
          }
  
          public string Test
          {
              get { return "SubTest"; }
              set { }
          }
  
  
          public bool IsExpanded
          {
              get { return m_IsExpanded; }
              set { Set(ref m_IsExpanded, value); }
          }
  
          private bool m_IsExpanded = false;
  
  
      }
  
  
      public class MyDataTest : ViewModelBase
      {
          public MyDataTest()
          {
              int test = 1000;
              for (int i = 0; i &lt; test; i++)
              {
                  Collection.Add(new MySubDataTest());
              }
          }
  
          public string Test
          {
              get { return "Test"; }
              set {  }
          }
  
  
          public bool IsExpanded
          {
              get { return m_IsExpanded; }
              set { Set(ref m_IsExpanded, value); }
          }
  
          private bool m_IsExpanded = false;
  
          public ObservableCollection&lt;MySubDataTest&gt; Collection
          {
              get { return m_Collection; }
          }
  
          ObservableCollection&lt;MySubDataTest&gt; m_Collection = new ObservableCollection&lt;MySubDataTest&gt;();
      }
  
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          public ObservableCollection&lt;MyDataTest&gt; Collection
          {
              get { return m_Collection; }
          }
  
          ObservableCollection&lt;MyDataTest&gt; m_Collection = new ObservableCollection&lt;MyDataTest&gt;();
  
          private void ButtonBase_OnClick(object _sender, RoutedEventArgs _e)
          {
              int count = 1;
              for (var i = 0; i &lt; count; i++)
              {
                      Collection.Add(new MyDataTest());
              }
              DataContext = this;
          }
  
          private void ButtonBase_OnClick2(object _sender, RoutedEventArgs _e)
          {
              foreach (MyDataTest test in Collection)
              {
                  foreach (MySubDataTest sub in test.Collection)
                  {
                      sub.IsExpanded = true;
                  }
                  test.IsExpanded = true;
              }
          }
      }
  }
  </code></pre>
  
  <p>Thank you in advance.</p>