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

  • SEARCH
  • Home
  • 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 6919051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:56:22+00:00 2026-05-27T09:56:22+00:00

I have a wpf treeview bound to collection. The model collection is of type:

  • 0

I have a wpf treeview bound to collection. The model collection is of type:

public Class A {
 public string Name {get; set}
 public ObservableCollection< B> Bs {get; set;}  
}

public Class B {  
  public ObservableCollection< C> Cs {get; set;}  
}

public Class C {
  public string Name {get; set;}
}

XAML:

<my:AConverter x:Key="AConverter"/>
<DataTemplate DataType="{x:Type C}">
 <TextBlock Text="{Binding Name}"/>
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type A}" 
     ItemsSource="{Binding Converter={StaticResource AConverter}}">
 <TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<treeView Grid.Row="0" ItemsSource="{Binding As}"/>

AConverter.cs:

public class AConverter: IValueConverter {

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {

        if (value is A) {
            return (value as A).Bs.SelectMany(b => b.Cs);
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        throw new NotImplementedException();
    }
}

The output expected is: A->C in treeview and not B. The issue here is when we add a new C the converter is not getting called to show the same in tree hierarchy

  • 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-27T09:56:22+00:00Added an answer on May 27, 2026 at 9:56 am

    You loose the updates if you apply a converter which does not return an observable collection [sic]. What you try to achieve is a bit of a pain really, basically you need to flatten the hierarchy while maintaining updates, this can be done using a CompositeCollection to some degree. e.g.

    public class AConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var compositeCollection = new CompositeCollection();
            var a = (A)value;
            // React to changes of the Bs:
            a.Bs.CollectionChanged += (s, e) =>
                {
                    switch (e.Action)
                    {
                        case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                            // ObservableCollection only ever manipulates one item, thus NewItems[0].
                            compositeCollection.Insert(e.NewStartingIndex, new CollectionContainer { Collection = ((B)e.NewItems[0]).Cs });
                            break;
                        case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                            //TODO: Implement
                            break;
                        case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                            //TODO: Implement
                            break;
                        case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                            //TODO: Implement
                            break;
                        case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
                            //TODO: Implement
                            break;
                        default:
                            break;
                    }
                };
            // Add the items for the current Bs:
            foreach (var item in a.Bs)
            {
                compositeCollection.Add(new CollectionContainer { Collection = item.Cs });
            }
            return compositeCollection;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    The implementation of the other cases is up to you, the insertions seem to work.

    A better solution might be to expose CompositeCollections at every level, because then they can easily be nested, i.e. you can add a CollectionContainer having a CompositeCollection as its source.

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

Sidebar

Related Questions

I have bound a WPF TreeView to an ObservableCollection. When a Connection node gets
I have a TreeView in WPF which I have bound to a collection of
In my WPF application, I have a treeview. This treeview is bound to a
I have a custom class that I would like to bind a WPF TreeView
I have the next WPF treeview: <TreeView HorizontalAlignment=Left Margin=6,0,0,32 Name=tvProductos Width=158> <TreeViewItem Header=Securities IsExpanded=True
I have a treeview in wpf that is built using the xaml below. It
Say I have a 3-level data-bound WPF TreeView like this one: a aa aaa
I have a TreeView implemented in WPF that is bound to some XML data
I have a class : Public Class treeModel Public Property title As String Public
I have a wpf application that displays a 'Node' as such: <TreeView HorizontalAlignment=Stretch Name=treeView1

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.