Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:21:41+00:00 2026-06-08T16:21:41+00:00

I need to know when a ListBox has finished rendering for the first time

  • 0

I need to know when a ListBox 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.

I have a ListBox that uses RichTextBox in it’s DataTemplate:

<DataTemplate x:Key="HelpTextTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        ...
        <ContentControl>
            ...
            <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"/>
            ...
        </ContentControl>
        ...
    </Grid>
</DataTemplate>

The ListBox is bound to an ObservableCollection.

I had a problem with the scrolling of the ListBox – if height of the RichTextBox was greater than that of the ListBox the user couldn’t scroll to the bottom of the RichTextBox. The ListBox 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 RichTextBox 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).

I tracked these problems down to the ListBox‘s use of a VirtualisingStackPanel to draw the items. When I replaced that with a simple StackPanel those problems went away.

This then created the problem I have now which is that the ListBox scrolls to the bottom of the list on initial load. The Loaded and LayoutUpdated events on the ListBox occur before the data has been loaded. I tried listening out for the PropertyChanged event on the view model when the ObservableCollection is initialised:

void editViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "ListDataSource":
            // Try to scroll to the top of the ListBox
            break;
    }
}

this fires too early as well. The list is rendered after this event is fired and causes the ListBox to scroll to the bottom.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:21:42+00:00Added an answer on June 8, 2026 at 4:21 pm

    In the end I had to use a kludge:

    private System.Threading.Timer timer;
    void editViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "ListDataSource":
                TimerCallback callback = TimerResult;
                timer = new Timer(callback, null, 750, 0);
                break;
        }
    }
    
    private void TimerResult(Object stateInfo)
    {
        Dispatcher.BeginInvoke(() =>
        {
            if (this.ItemsList.Items.Count > 0)
            {
                this.ItemsList.UpdateLayout();
                this.ItemsList.SelectedIndex = 0;
                this.ItemsList.ScrollIntoView(this.ItemsList.Items[0]);
            }
        });
    
        timer.Dispose();
    }
    

    If anyone knows a better way please post your answer now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a listbox that selects on first click and un-selects on second click,
I know that this is not the first time a question about this issue
Need to know this so that i could send DTMF and that is going
I need to know how to change background of expanded list item when it
I need to know a couple of things, concerning IRC servers that I couldnt
You know Visual Studio, that awesome element called ListBox? Just a box that would
I need to update a ListBox of a Form2 created dynamically. Let's say that
How can I remove duplicate items from ListBox in Delphi? I know this: for
Need to know which event triggered after text selection done in Apple iPad. It
I need to know how to exclude files/folders from php generated zip. Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.