Answered Nested TreeView Heirarchy Generation

  • Friday, July 27, 2012 8:16 PM
     
      Has Code

    If the following code produces this tree:

    The Flanders

        Ned

        Maude

        Todd

        Rod

    what code would produce this tree?

    The Flanders

        Ned

             Maude

                    Todd

                     Rod

        Frank

    The Simpsons

        Homer   

    ??

        public class FlandersViewModel
        {
            public FlandersViewModel()
            {
    
                Name = "The Flanders";
    
                Children = new ObservableCollection<Person>();
    
                Children.Add(new Person { Name = "Ned" });
                Children.Add(new Person { Name = "Maude" });
                Children.Add(new Person { Name = "Todd" });
                Children.Add(new Person { Name = "Rod" });
            }
    
            public string Name { get; set; }
    
            public ObservableCollection<Person> Children { get; set; }
    
        }
    
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
    
        }
    }


    REvans


    • Edited by REvans611 Friday, July 27, 2012 8:19 PM
    •  

All Replies

  • Saturday, July 28, 2012 12:47 AM
     
     Answered Has Code

    Hi

    You need to add the "Children" collection property also to the "Person" class
    to enable a family tree with more than one level of (grand)children.

    The tree is built up by adding persons to other persons Children
    collectiion recursively.

    Chris

    For example:


    using System.Collections.ObjectModel;
    using System.Windows;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeData();
                InitializeComponent();
            }
            private void InitializeData()
            {
                //Build Family trees
                var flanders = new Family { Name = "The Flanders" };
                var ned = new Person { Name = "Ned" };
                var maude = new Person { Name = "Maude" };
                var todd = new Person { Name = "Todd" };
                var rod = new Person { Name = "Rod" };
    
                maude.Children.Add(todd);
                maude.Children.Add(rod);
                ned.Children.Add(maude);
                flanders.Children.Add(ned);
    
                var simpsons = new Family { Name = "The Simpsons" };
                var homer = new Person { Name = "Homer" };
                simpsons.Children.Add(homer);
                
                var viewModel = new MainViewModel();
                viewModel.Families.Add(flanders);
                viewModel.Families.Add(simpsons);
         
                DataContext = viewModel;
            }
        }
        public class MainViewModel //TODO impl. INotifyPropertyChanged
        {
            public MainViewModel()
            {
                Families = new ObservableCollection<Family>();
            }
            public ObservableCollection<Family> Families { get; private set; }
        }
        public class Family : NodeBase
        {
        }
        public class Person : NodeBase
        {
            public int Age { get; set; }
        }
        public abstract class NodeBase
        {
            protected NodeBase()
            {
                Children = new ObservableCollection<Person>();
            }
            public string Name { get; set; }
            public ObservableCollection<Person> Children { get; private set; }
        }
    }



    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <TreeView ItemsSource="{Binding Path=Families}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate  ItemsSource="{Binding Path=Children}">
                    <TextBlock Text="{Binding Path=Name}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
            <TreeView.ItemContainerStyle>
                <Style>
                    <Setter Property="TreeViewItem.IsExpanded" Value="True" />
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>
    </Window>








  • Saturday, July 28, 2012 9:32 PM
     
     
    Thank you, very much Christian Developer.  That was exactly what I was looking for.

    REvans