title: ObservableCollection and ListBoxItem DataTemplate generation problem
body: <p>Something strange is going on with ObservableCollection.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>private readonly ObservableCollection&lt;DisplayVerse&gt; _display;
  private readonly ListBox _box;
  
      private void TransferToDisplay()
      {
          double elementsHeight = 0;
  
          _display.Clear();
  
          for (int i = 0; i &lt; _source.Count; i++) {
              DisplayVerse verse = _source[i];
              _display.Add(verse);
              elementsHeight += CalculateItemsHeight(i);
              if (elementsHeight + Offset &gt; _box.ActualHeight) {
                  _display.RemoveAt(_display.Count - 1);
                  break;
              }
          }
          MessageBox.Show(elementsHeight.ToString());
      }
  
      private double CalculateItemsHeight(int index)
      {
          ListBoxItem lbi = _box.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;
          return lbi != null ? lbi.ActualHeight : 0;
      }
  </code></pre>
  
  <p>What I am trying to do here is control how many items go into the ObservableCollection _display. Now, within this for loop you can see that elements are added until the total elements height (+offset) is greater than the listbox itself.</p>
  
  <p>Now, this is strange, the elementsHeight equals 0 after this for loop. (CalculateItemsHeight returns 0 in all for loop iterations even though the lbi is not null) It seems that the UI elements defined in the datatemplate are not created...</p>
  
  <p>Yet.</p>
  
  <p>Now, if I put some MessageBoxes after the _display.Add(verse) you can see that the CalculateItemsHeight actually returns the height of an item.</p>
  
  <pre><code>for (int i = 0; i &lt; _source.Count; i++) {
      DisplayVerse verse = _source[i];
      _display.Add(verse);
      MessageBox.Show("pause"); // &lt;----- PROBLEM?
      elementsHeight += CalculateItemsHeight(i);
      if (elementsHeight + Offset &gt; _box.ActualHeight) {
          _display.RemoveAt(_display.Count - 1);
          break;
      }
  }
  MessageBox.Show(elementsHeight.ToString());
  </code></pre>
  
  <p>After I modify the for loop as shown, the last MessageBox <strong>actually shows</strong> the actual height for all processed elements.</p>
  
  <p>My question is - <strong>when are the UI elements actually created</strong>? It seems that it was done somewhere during the MessageBox display. This behaviour is pretty strange for me, maybe it has something to do with threading, not sure.</p>
  
  <p>Adding to the _display ObservableCollection obviously creates an item immediately, but not its visual elements (they are however added afterwards, I just don't know exactly when). How can I do this same behaviour without having to pop the message box up?</p>