title: Add an item to ListBox and get the ListBoxItem
body: <p><em><strong>UPDATE:</em></strong></p>
  
  <p>I uploaded a demo trying to explain my problem clear. Download it <a href="https://www.box.com/s/5gltokm5wreara2xg19k" rel="nofollow">here</a>.</p>
  
  <p>I'm developing an manager class that deals with the selection of ListBox. (The default selection function provided by ListBox cannot satisfy me requirements)</p>
  
  <p>So, <strong>when an item is added to the ListBox, my manager class should get the corresponding ListBoxItem</strong> and make it selected or unselected.</p>
  
  <p>Although I think <strong><code>ItemContainerGenerator.ItemsChanged</code> should tell some information of the newly added item, it provides same event arg when <code>ListBox.Items.Add</code> is called multiple times(with different parameter)</strong>, which confuses me a lot. Can anyone tell me how to get the newly generated ListBoxItem for the newly added item.</p>
  
  <p>Code to demonstrate the problem:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication2.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;StackPanel&gt;
          &lt;Button Content="Add two items" Click="Button_Click_1"/&gt;
          &lt;ListBox Name="listBox"&gt;
              &lt;System:Int32&gt;1&lt;/System:Int32&gt;
              &lt;System:Int32&gt;2&lt;/System:Int32&gt;
              &lt;System:Int32&gt;3&lt;/System:Int32&gt;
          &lt;/ListBox&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
  
          SelectionManager selectionManager = new SelectionManager();
          selectionManager.Join(listBox);
      }
  
      private void Button_Click_1(object sender, RoutedEventArgs e)
      {
          listBox.Items.Add(4);
          listBox.Items.Add(5);
      }
  }
  </code></pre>
  
  <p>Here, in Button_Click, two items are added to the listBox, and selectionManager should get the ListBoxItem at same time.</p>
  
  <pre><code>class SelectionManager
  {
      public void Join(ListBox element)
      {
          element.ItemContainerGenerator.ItemsChanged += ItemContainerGenerator_ItemsChanged;
      }
  
      private List&lt;int&gt; listBoxItemPendingJoinIndexes = new List&lt;int&gt;();
  
      void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
      {
          Contract.Requires(listBoxItemPendingJoinIndexes.Count &gt; 0);
          ItemContainerGenerator generator = (ItemContainerGenerator)sender;
          if (generator.Status != GeneratorStatus.ContainersGenerated)
              return;
          generator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  
          foreach (var index in listBoxItemPendingJoinIndexes)
          {
              ListBoxItem listBoxItem = (ListBoxItem)generator.ContainerFromIndex(index);
              Join(listBoxItem);
          }
          listBoxItemPendingJoinIndexes.Clear();
      }
  
      void ItemContainerGenerator_ItemsChanged(object sender, ItemsChangedEventArgs e)
      {
          ItemContainerGenerator generator = (ItemContainerGenerator)sender;
          switch (e.Action)
          {
              case NotifyCollectionChangedAction.Add:
                  listBoxItemPendingJoinIndexes.Add(e.Position.Index
                        + e.Position.Offset);//same e.Position
                  generator.StatusChanged += ItemContainerGenerator_StatusChanged;
                  break;
          }
  
      }
  }
  </code></pre>