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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:27:19+00:00 2026-05-21T05:27:19+00:00

The follow code loads a hierarchical collection for of CompoundObjects then of strings. But

  • 0

The follow code loads a hierarchical collection for of CompoundObjects then of strings. But unfortunately it inserts the strings at the top of the tree instead of at the bottom(which is the behavior I’ve always seen.

I’ve tried to change the order of the enumerators, the notifiers, etc and all produce the same results. I’ve I pre-load the list it looks normal(using the same code in the thread).

Any ideas whats going on?

CompoundObject.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

namespace ComplexTreeViewLazyLoadingTest
{
    public class CompoundObject : IEnumerable<object>, INotifyCollectionChanged
    {
        public string Name { get; set; }
        public ObservableCollection<CompoundObject> objects { get; private set; }
        public ObservableCollection<string> Items { get; private set; }

        void OnChanged(object sender, NotifyCollectionChangedEventArgs e) 
        { 
            if (CollectionChanged != null) 
            App.Current.Dispatcher.Invoke((Action<object, NotifyCollectionChangedEventArgs>)((senderr, ee) => {
                CollectionChanged(senderr, ee); 
            }), sender, e);
        }

        public CompoundObject(string name) 
        { 
            Name = name; 
            Items = new ObservableCollection<string>();
            objects = new ObservableCollection<CompoundObject>();

            Items.CollectionChanged += new NotifyCollectionChangedEventHandler(OnChanged);
            objects.CollectionChanged += new NotifyCollectionChangedEventHandler(OnChanged);
        }


        public IEnumerator<object> GetEnumerator()
        {
            if (objects != null) foreach(var a in objects) yield return a;
            if (Items != null) foreach (var a in Items) yield return a;         
            yield break;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }



        public event NotifyCollectionChangedEventHandler CollectionChanged;

    }



}

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace ComplexTreeViewLazyLoadingTest
{


    public partial class MainWindow : Window
    {

        CompoundObject c = new CompoundObject("Root");
        public MainWindow()
        {
            InitializeComponent();

            treeView.DataContext = c;
            treeView.ItemsSource = c;

            ThreadPool.QueueUserWorkItem(new WaitCallback(Update));

        }


        void Update(object data)
        {


            for (int i = 0; i < 10; i++)
            {
                Application.Current.Dispatcher.Invoke((Action<CompoundObject>)((cc) => {
                    c.objects.Add(cc);
                }), new CompoundObject("Object " + i));

                for (int j = 0; j < 5; j++)
                {
                    Thread.Sleep(100);
                    Application.Current.Dispatcher.Invoke((Action<CompoundObject>)((cc) =>
                    {
                        c.objects[i].objects.Add(cc);
                    }), new CompoundObject("subObject " + j));

                }

            }

            for (int i = 0; i < 8; i++)
            {
                Thread.Sleep(250);
                Application.Current.Dispatcher.Invoke((Action<string>)((ii) =>
                {
                    c.Items.Add("Item " + ii);
                }), i.ToString());
            }



        }

    } // MainWindow



    public class DTS : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;

            if (element != null && item != null)
            {

                if (item is CompoundObject)
                {
                    return element.FindResource("CompoundTemplate") as DataTemplate;
                }

                if (item is int)
                {
                    return element.FindResource("DefaultTemplate") as DataTemplate;
                }
            }



            return null;
        }
    }


}

MainWindow.xaml

<Window x:Class="ComplexTreeViewLazyLoadingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ComplexTreeViewLazyLoadingTest;assembly="
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <local:DTS x:Key="DTS"/>

        <HierarchicalDataTemplate x:Key="CompoundTemplate" ItemsSource="{Binding Path=.}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate x:Key="DefaultTemplate" ItemsSource="{Binding Path=.}">
            <TextBlock Text="{Binding Path=.}" Background="Aqua" />
        </HierarchicalDataTemplate>

    </Window.Resources>


        <Grid>


        <TreeView Name="treeView" ItemTemplateSelector="{StaticResource DTS}"/>

    </Grid>
</Window>
  • 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-21T05:27:20+00:00Added an answer on May 21, 2026 at 5:27 am

    Because you’re combining two collections and subscribing to the CollectionChanged directly, the change notifications are for the sub-lists instead of the ‘combined’ list. This means that you’ll get a notification that ‘string added at 0’ when really you want it added at the end of the list. In order to make this work, you’ll need to subscribe to the CollectionChanged for each sub-collection and implement your own CollectionChanged callback properly (to add the first collection’s Count to all indices reported when adding/removing strings.)

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

Sidebar

Related Questions

I have this follow code in my javascript. I call this function when the
i have the follow code: searchResults.SearchResultCollection.Add( new SearchResult() { Header = HttpUtility.HtmlDecode( htmlDocument.DocumentNode .SelectSingleNode(initialXPath
Consider the follow code: TMyList = class(TList<IMyItem>, IMyList) Delphi shows me the error: [DCC
Hi i am having the follow code <div id=container> <div class=element NOTME></div> <div class=element></div>
This has me stumped. The follow code returns ,,,,,,: <script type=text/javascript> $(function() { $('#listB').sortable({
I had created a zip file (together with directory) under Windows as follow (Code
So... is strange, I have the follow code in my Window control for setting
I am new to VB. I read a book and follow its code Public
I user code as follow to get album id in facebook and i get
I have strings like follow field_zip_code:48103 taxonomy:88 field_zip_code:48103 taxonomy:88 field_state:MI field_zip_code:48103 From here i

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.