0

Instead of saving the result to a string in here, I decided to use a TreeView to display it.

Here is the result I get:

MyBottle:
      BottleName: Big bottle
      BottageAge: 2    
SpecialFolders:
            TemplateFolder: Templates
            UserFolder: UserProfile

However, I am expecting to get something like:

MyBottle:
      BottleName: Big bottle
      BottageAge: 2
Addresses:
      AddressLine1: 1 Main St
      AddressLine2: 2 Main St
      SpecialFolders:
            TemplateFolder: Templates
            UserFolder: UserProfile

Here is the code: (The relevent part is method PrintProperties())

<Window x:Class="TreeViewExample.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">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TreeView x:Name="treeview" Grid.Row="1" HorizontalAlignment="Stretch"   VerticalAlignment="Stretch" /> 
    </Grid>
</Window>

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

namespace TreeViewExample
{
    public class Container
    {
        public Bottle MyBottle { get; set; }
        public List<Address> Addresses { get; set; }

        public Container()
        {
            Address a = new Address();
            a.AddressLine1 = "1 Main St";
            a.AddressLine2 = "2 Main St";
            Addresses = new List<Address>();
            Addresses.Add(a);

            MyBottle = new Bottle();
            MyBottle.BottleName = "Big bottle";
            MyBottle.BottageAge = 2;
        }
    }

    public class Bottle
    {
        public string BottleName { get; set; }
        public int BottageAge { get; set; }
    }

    public class Address
    {
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public List<SpecialFolder> SpecialFolders { get; set; }

        public Address()
        {
            SpecialFolders = new List<SpecialFolder>();
            SpecialFolder sf = new SpecialFolder();
            sf.TemplateFolder = Environment.SpecialFolder.Templates.ToString();
            sf.UserFolder = Environment.SpecialFolder.UserProfile.ToString();
            SpecialFolders.Add(sf);
        }
    }

    public class SpecialFolder
    {
        public string TemplateFolder { get; set; }
        public string UserFolder { get; set; }
    }
}


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.Reflection;
using System.Collections;

namespace TreeViewExample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            TreeViewItem containerTree = new TreeViewItem();

            Container c = new Container();
            PrintProperties(c, containerTree, containerTree, containerTree, false);

            containerTree.Header = "Container of everything";
            treeview.Items.Add(containerTree);

        }

        public void PrintProperties(object obj, TreeViewItem propOfContainerTree, TreeViewItem tree, TreeViewItem storage, bool lastIterationInSecondLoop)
        {
            if (obj == null) return;
            Type objType = obj.GetType();
            PropertyInfo[] properties = objType.GetProperties();
            PropertyInfo last = properties.Last();

            foreach (PropertyInfo property in properties)
            {
                object propValue = property.GetValue(obj, null);
                var elems = propValue as IList;
                int i = 0;
                if (elems != null)
                {
                    var lastIndex = elems.Count;
                    foreach (var elem in elems)
                    {
                        ++i;
                        propOfContainerTree = new TreeViewItem() { Header = property.Name };
                        if (lastIndex != i)
                            PrintProperties(elem, propOfContainerTree, tree, storage, false);
                        else
                            PrintProperties(elem, propOfContainerTree, tree, storage, true);
                    }

                }
                else
                {
                    if (property.PropertyType.Assembly == objType.Assembly)
                    {
                        propOfContainerTree = new TreeViewItem() { Header = property.Name };
                        //if (typeof(List).IsAssignableFrom(propOfContainerTree.GetType()))
                        //{
                        //    storage = propOfContainerTree;
                        //}
                        PrintProperties(propValue, propOfContainerTree, tree, storage, false);
                    }
                    else
                    {
                        TreeViewItem propOfProfOfContainerTree = new TreeViewItem() { Header = property.Name + ": " + propValue };
                        propOfContainerTree.Items.Add(propOfProfOfContainerTree);

                        if (property.Equals(last))
                        {
                            try
                            {
                                storage.Items.Add(propOfContainerTree);
                            }
                            catch { }

                            if (lastIterationInSecondLoop)
                            {
                                tree.IsExpanded = true; 
                                propOfContainerTree.IsExpanded = true; 
                            }
                        }
                    }
                }
            }
        }
    }
}

I think the problem lies in my TreeViewItem storage, which causes the "Addresses" to disappear. Any input would be great appreciated.

Community
  • 1
  • 1
HoKy22
  • 4,057
  • 8
  • 33
  • 54
  • `Any input would be great appreciated.` - Delete all your code and create a proper Data Model to store your data. Then use [DataBinding](http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx) to populate the UI with that data. [UI is Not Data](http://stackoverflow.com/a/14382137/643085), nor is it the right place to store data. Learn MVVM before you ever write a single line of code in WPF. [Here](http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode) is how to implement a TreeView in WPF the right way. – Federico Berasategui Dec 16 '13 at 20:10
  • I saw that TreeView WPF article before; that's not what I wanted. I wanted to use ystem.Reflection so that all the properties can be obtained by recursion. – HoKy22 Dec 16 '13 at 20:26
  • 1
    My point still stands. Reflection or not Reflection, remove all that from code behind, and don't mix your business logic with UI related stuff. – Federico Berasategui Dec 16 '13 at 20:28

0 Answers0