C# 使用递归重新感染的TreeView显示

C# 使用递归重新感染的TreeView显示,c#,wpf,reflection,recursion,treeview,C#,Wpf,Reflection,Recursion,Treeview,我决定使用树状视图来显示结果,而不是将结果保存到中的字符串中 以下是我得到的结果: MyBottle: BottleName: Big bottle BottageAge: 2 SpecialFolders: TemplateFolder: Templates UserFolder: UserProfile 但是,我希望得到如下结果: MyBottle: BottleName: Big bottle

我决定使用树状视图来显示结果,而不是将结果保存到中的字符串中

以下是我得到的结果:

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; 
                            }
                        }
                    }
                }
            }
        }
    }
}

我认为问题在于我的TreeView项目存储,它导致地址消失。任何意见都将不胜感激。

任何意见都将不胜感激。-删除所有代码并创建适当的数据模型来存储数据。然后使用以使用该数据填充UI,它也不是存储数据的正确地方。在用WPF编写一行代码之前,先学习MVVM。是如何在WPF中实现TreeView的正确方法;那不是我想要的。我想使用System.Reflection,以便通过递归获得所有属性。我的观点仍然成立。不管是反射还是非反射,都要从代码中删除所有这些内容,不要将业务逻辑与UI相关的东西混为一谈。