title: How do I know when a ListBox has finished rendering in Silverlight?
body: <p>I need to know when a <code>ListBox</code> has finished rendering for the first time so that I can scroll it to the top to present the user with the first item on the list.</p>
  
  <p>I have a <code>ListBox</code> that uses <code>RichTextBox</code> in it's <code>DataTemplate</code>:</p>
  
  <pre><code>&lt;DataTemplate x:Key="HelpTextTemplate"&gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="*"/&gt;
              &lt;ColumnDefinition Width="Auto"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          ...
          &lt;ContentControl&gt;
              ...
              &lt;RichTextBox x:Name="HelpTextContent" Grid.Row="1"
                           Tag="{Binding Path=HelpObject.Text, Mode=TwoWay}"
                           TextWrapping="Wrap"
                           HorizontalAlignment="Stretch"
                           Margin="0,0,20,0"
                           Loaded="RichTextBox_Loaded"
                           ContentChanged="RichTextBox_ContentChanged"
                           SelectionChanged="RichTextBox_SelectionChanged"/&gt;
              ...
          &lt;/ContentControl&gt;
          ...
      &lt;/Grid&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>The <code>ListBox</code> is bound to an <code>ObservableCollection</code>.</p>
  
  <p>I had a problem with the scrolling of the <code>ListBox</code> - if height of the <code>RichTextBox</code> was greater than that of the <code>ListBox</code> the user couldn't scroll to the bottom of the <code>RichTextBox</code>. The <code>ListBox</code> would jump to the next item in the list. The height of the scroll bar's slider would change as well. This is because the actual height of the <code>RichTextBox</code> is only calculated when it's actually rendered. When it's off the screen the height reverts to smaller value (I think the code assumes that the text can all fit on a single line rather than having to be wrapped).</p>
  
  <p>I tracked these problems down to the <code>ListBox</code>'s use of a <code>VirtualisingStackPanel</code> to draw the items. When I replaced that with a simple <code>StackPanel</code> those problems went away.</p>
  
  <p>This then created the problem I have now which is that the <code>ListBox</code> scrolls to the bottom of the list on initial load. The <code>Loaded</code> and <code>LayoutUpdated</code> events on the <code>ListBox</code> occur before the data has been loaded. I tried listening out for the <code>PropertyChanged</code> event on the view model when the <code>ObservableCollection</code> is initialised:</p>
  
  <pre><code>void editViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
      switch (e.PropertyName)
      {
          case "ListDataSource":
              // Try to scroll to the top of the ListBox
              break;
      }
  }
  </code></pre>
  
  <p>this fires too early as well. The list is rendered <strong>after</strong> this event is fired and causes the <code>ListBox</code> to scroll to the bottom.</p>