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 5979965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:44:06+00:00 2026-05-22T21:44:06+00:00

Scenario I have a TreeView that is bound to ObservableCollection<T> . The collection gets

  • 0

Scenario

I have a TreeView that is bound to ObservableCollection<T>. The collection gets modified every time the end-user modifies their filters. When users modify their filters a call to the database is made (takes 1-2ms tops) and the data returned gets parsed to create a hierarchy. I also have some XAML that ensures each TreeViewItem is expanded, which appears to be part of the problem. Keep in mind that I’m only modifying ~200 objects with a max node depth of 3. I would expect this to instant.

Problem

The problem is that whenever filters get modified and the TreeView hierarchy gets changed the UI hangs for ~1 second.

Here is the XAML responsible for create the TreeView hierarchy.

<TreeView VerticalAlignment="Top" ItemsSource="{Binding Hierarchy}" Width="240"
          ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1">
    <TreeView.ItemTemplate>
        <!-- Hierarchy template -->
        <HierarchicalDataTemplate ItemsSource="{Binding Stations}">
            <TextBlock Text="{Binding}" />
            <!-- Station template -->
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Locates}">
                    <TextBlock Text="{Binding Name}" />
                    <!-- Locate template -->
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding TicketNo}" />
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

And here is the code for updating the list.

public ObservableCollection<HierarchyViewModel> Hierarchy
{
    get { return _hierarchy; }
    set { _hierarchy = value; }
}

public void UpdateLocates(IList<string> filterIds)
{
    _hierarchy.Clear();

    // Returns 200 records max    
    var locates = _locateRepository.GetLocatesWithFilters(filterIds);
    var dates = locates.Select(x => x.DueDate);

    foreach (var date in dates)
    {
        var vm = new HierarchyViewModel
        {
            DueDate = date
        };
        var groups = locates.Where(x => x.DueDate.Date.Equals(date.Date)).GroupBy(x => x.SentTo);

        // Logic ommited for brevity

        _hierarchy.Add(vm);
    }
}

I also have <Setter Property="IsExpanded" Value="True" /> as a style. I have tried using a BindingList<T> and disabling notifications, but that didn’t help.

Any ideas as to why my UI hangs whenever changes are made to the ObservableCollection<T>?

Partial Solution

With what H.B. said and implementing a BackgroundWorker the update is much more fluid.

  • 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-05-22T21:44:06+00:00Added an answer on May 22, 2026 at 9:44 pm

    The problem is probably the foreach loop. Every time you add an object the CollectionChanged event is fired and the tree is rebuilt.

    You do not want to use an ObservableCollection if all you do is clear the whole list and replace it with a new one, use a List and fire a PropertyChanged event once the data is fully loaded.

    i.e. just bind to a property like this (requires implementation of INotifyPropertyChanged):

    private IEnumerable<HierarchyViewModel> _hierarchy = null;
    public IEnumerable<HierarchyViewModel> Hierarchy
    {
        get { return _hierarchy; }
        set
        {
            if (_hierarchy != value)
            {
                _hierarchy = value;
                NotifyPropertyChanged("Hierarchy");
            }
        }
    }
    

    If you set this property bindings will be notified. Here i use the IEnumerable interface so no-one tries to just add items to it (which would not be noticed by the binding). But this is just one suggestion which may or may not work for your specific scenario.

    (Also see sixlettervariable’s good comment)


    Just a side note, this code:

    public ObservableCollection<HierarchyViewModel> Hierarchy
    {
        get { return _hierarchy; }
        set { _hierarchy = value; }
    }
    

    is bad, you could overwrite the list and the binding would break because there is no PropertyChanged event being fired in the setter.

    If you use an ObservableCollection it normally is used like this:

    private readonly ObservableCollection<HierarchyViewModel> _hierarchy =
                new ObservableCollection<HierarchyViewModel>();
    public ObservableCollection<HierarchyViewModel> Hierarchy
    {
        get { return _hierarchy; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: You have an ASP.Net webpage that should display the next image in a
Scenario: I have a text file that has pipe (as in the | character)
The scenario: we have a web system that automatically generates office 2003 excel files
Here's my scenario - I have an SSIS job that depends on another prior
Here's the scenario: You have a Windows server that users remotely connect to via
I have an Application layout that is based on a treeView at the left,
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Scenario: I have a customer object with lazy loading enabled. I use that throughout
Scenario: I have an application that pulls data from a SQL database as well
My scenario: I have a background thread that polls for changes and periodically updates

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.