title: How does ItemContainerGenerator.ContainerFromItem work with a grouped list?
body: <p>I have a ListBox which until recently was displaying a flat list of items. I was able to use myList.ItemContainerGenerator.ConainerFromItem(thing) to retrieve the ListBoxItem hosting "thing" in the list.</p>
  
  <p>This week I've modified the ListBox slightly in that the CollectionViewSource that it binds to for its items has grouping enabled. Now the items within the ListBox are grouped underneath nice headers.</p>
  
  <p>However, since doing this, ItemContainerGenerator.ContainerFromItem has stopped working - it returns null even for items I know are in the ListBox. Heck - ContainerFromIndex(0) is returning null even when the ListBox is populated with many items!</p>
  
  <p>How do I retrieve a ListBoxItem from a ListBox that's displaying grouped items?</p>
  
  <p>Edit: Here's the XAML and code-behind for a trimmed-down example. This raises a NullReferenceException because ContainerFromIndex(1) is returning null even though there are four items in the list.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
      Title="Window1"&gt;
  
      &lt;Window.Resources&gt;
          &lt;XmlDataProvider x:Key="myTasks" XPath="Tasks/Task"&gt;
              &lt;x:XData&gt;
                  &lt;Tasks xmlns=""&gt;
                      &lt;Task Name="Groceries" Type="Home"/&gt;
                      &lt;Task Name="Cleaning" Type="Home"/&gt;
                      &lt;Task Name="Coding" Type="Work"/&gt;
                      &lt;Task Name="Meetings" Type="Work"/&gt;
                  &lt;/Tasks&gt;
              &lt;/x:XData&gt;
          &lt;/XmlDataProvider&gt;
  
          &lt;CollectionViewSource x:Key="mySortedTasks" Source="{StaticResource myTasks}"&gt;
              &lt;CollectionViewSource.SortDescriptions&gt;
                  &lt;scm:SortDescription PropertyName="@Type" /&gt;
                  &lt;scm:SortDescription PropertyName="@Name" /&gt;
              &lt;/CollectionViewSource.SortDescriptions&gt;
  
              &lt;CollectionViewSource.GroupDescriptions&gt;
                  &lt;PropertyGroupDescription PropertyName="@Type" /&gt;
              &lt;/CollectionViewSource.GroupDescriptions&gt;
          &lt;/CollectionViewSource&gt;
      &lt;/Window.Resources&gt;
  
      &lt;ListBox 
          x:Name="listBox1" 
          ItemsSource="{Binding Source={StaticResource mySortedTasks}}" 
          DisplayMemberPath="@Name"
          &gt;
          &lt;ListBox.GroupStyle&gt;
              &lt;GroupStyle&gt;
                  &lt;GroupStyle.HeaderTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock Text="{Binding Name}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/GroupStyle.HeaderTemplate&gt;
              &lt;/GroupStyle&gt;
          &lt;/ListBox.GroupStyle&gt;
      &lt;/ListBox&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>CS:</p>
  
  <pre><code>public Window1()
  {
      InitializeComponent();
      listBox1.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
  }
  
  void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
  {
      if (listBox1.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
      {
          listBox1.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  
          var i = listBox1.ItemContainerGenerator.ContainerFromIndex(1) as ListBoxItem;
  
          // select and keyboard-focus the second item
          i.IsSelected = true;
          i.Focus();
      }
  }
  </code></pre>