使用递归反射的TreeView显示

时间:2013-12-16 19:33:27

标签: c# wpf reflection recursion treeview

我没有将结果保存到here中的字符串,而是决定使用TreeView来显示它。

以下是我得到的结果:

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

但是,我期待得到类似的东西:

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

以下是代码:(相关部分是方法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; 
                            }
                        }
                    }
                }
            }
        }
    }
}

我认为问题出在我的TreeViewItem存储空间中,导致"Addresses"消失。任何意见都会非常感激。

0 个答案:

没有答案