如何从对象列表中填充树形视图

10

我有一个问题,无法将我的对象列表填充到我的树形视图中。我一直在谷歌上寻找解决方案,找到了一些与我的问题接近的主题,但没有一个能够解决它。

我有一个包含每个对象属性名称和组的列表。

我想像下面这样填充我的树形视图:

+---Group 1
|   |
|   +--------object.Name <-- 
|   +--------object.Name <-- all objects with object.Group = Group 1
|   +--------object.Name <--
|
+---Group 2
|   |
|   +--------object.Name <-- 
|   +--------object.Name <-- all objects with object.Group = Group 2
|   +--------object.Name <--
|

等等还有其他的。

谢谢。


1
请发布你的代码,生成对象列表,然后我会为你处理treeview语句 :) - saeed
5个回答

4

工作得非常好,谢谢。我只是在else的开头和结尾添加了几行代码,如下所示。

private void PopulateTreeView()
    {
        ListOfObjectsSorted = ListOfObjects.OrderBy(r => r.Group).ToList();
        var topNode = new TreeNode("Select all");
        treeView1.Nodes.Add(topNode);
        string currentGroup = ListOfObjectsSorted.First().Group;
        var treeNodes = new List<TreeNode>();
        var childNodes = new List<TreeNode>();
        foreach (Object obj in ListOfObjectsSorted )
        {
            if (currentGroup == rule.Group)
                childNodes.Add(new TreeNode(obj.Name));
            else
            {
                if (childNodes.Count > 0)
                {
                    treeNodes.Add(new TreeNode(currentGroup, childNodes.ToArray()));
                    childNodes = new List<TreeNode>();
                }
                childNodes.Add(new TreeNode(obj.Name));
                currentGroup = obj.Group;
            }
        }
        if (childNodes.Count > 0)
        {
            treeNodes.Add(new TreeNode(currentGroup, childNodes.ToArray()));
        }
        treeView1.Nodes[0].Nodes.AddRange(treeNodes.ToArray());
    }

2

您的问题是技术性问题还是如何处理它?您可以按照object.Group进行排序,然后每当组发生变化时创建一个顶层节点。

编辑: 以下是一些示例代码,我尚未测试,因此请将其视为指南:

string currentGroup = null;
List<TreeNode> treeNodes = new List<TreeNode>();
List<TreeNode> childNodes = new List<TreeNode>();
foreach (BusinessObject obj in objectList)
{
    if (currentGroup == obj.Group)
        childNodes.Add(new TreeNode(obj.Name));
    else
    {
        if (childNodes.Count > 0)
        {
            treeNodes.Add(new TreeNode(currentGroup, childNodes.ToArray()));
            childNodes = new List<TreeNode>();
        }
        currentGroup = obj.Group;
    }
}
if (childNodes.Count > 0)
{
    treeNodes.Add(new TreeNode(currentGroup, childNodes.ToArray()));
}
treeView.Nodes.AddRange(treeNodes.ToArray());

我的问题更多的是技术性的。我按照你说的排序了,但是我不知道如何在每次分组更改时创建一个顶级节点。 - user2346200

1
这里是一些VB代码...
尝试(Try)
        Me.Cursor = Cursors.WaitCursor
        If rtb.Visible Then line = rtb.Lines
        Dim i, j As Integer
        Dim myLst As New ListBox
        Dim nod0 As New TreeNode
        Dim nod1 As New TreeNode
        Dim nodGet As New TreeNode
        Dim nodSet As New TreeNode
        'Aktualna pozicia
        If trw.SelectedNode Is Nothing Then
        Else
            nodGet = trw.SelectedNode
        End If

        For Each nod0 In trw.Nodes
            If nod0.IsExpanded Then
                myLst.Items.Add(nod0.Tag)
                For Each nod1 In nod0.Nodes
                    If nod1.IsExpanded Then myLst.Items.Add(nod1.Tag)
                Next
            End If
        Next

        Dim cntr() As Integer = {-1, -1, -1}

        trw.Nodes.Clear()
        trw.ShowPlusMinus = False
        trw.ShowRootLines = False
        trw.CheckBoxes = False
        For i = 0 To UBound(line)
            If Mid(line(i), 1, 1) = "\" Then
                j = line(i).IndexOf(dod)
                If j > 0 Then
                    s = Mid(line(i), 1, j)
                Else
                    s = line(i)
                End If
                If Mid(s, 1, 4) = "\\\\" Then
                    trw.Nodes.Add(Mid(s, 5))
                    cntr(0) = cntr(0) + 1
                    cntr(1) = -1
                    cntr(2) = -1
                    trw.Nodes(cntr(0)).Tag = s
                    trw.Nodes(cntr(0)).ImageIndex = 4
                    trw.Nodes(cntr(0)).SelectedImageIndex = 5
                    If trw.Nodes(cntr(0)).Tag = nodGet.Tag Then nodSet = trw.Nodes(cntr(0))
                Else
                    If Mid(s, 1, 3) = "\\\" Then
                        If cntr(0) = -1 Then trw.Nodes.Add("...") : cntr(0) = 0 : cntr(1) = -1
                        trw.Nodes(cntr(0)).Nodes.Add(Mid(s, 4))
                        cntr(1) = cntr(1) + 1
                        cntr(2) = -1
                        trw.Nodes(cntr(0)).Nodes(cntr(1)).Tag = trw.Nodes(cntr(0)).Tag.ToString & eol & s
                        trw.Nodes(cntr(0)).Nodes(cntr(1)).ImageIndex = 1
                        trw.Nodes(cntr(0)).Nodes(cntr(1)).SelectedImageIndex = 2
                        If trw.Nodes(cntr(0)).Nodes(cntr(1)).Tag = nodGet.Tag Then nodSet = trw.Nodes(cntr(0)).Nodes(cntr(1))
                    Else
                        If Mid(s, 1, 2) = "\\" Then
                            If cntr(0) = -1 Then trw.Nodes.Add("...") : cntr(0) = 0 : cntr(1) = -1
                            If cntr(1) = -1 Then trw.Nodes(cntr(0)).Nodes.Add("...") : cntr(1) = 0 : cntr(2) = -1
                            trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes.Add(Mid(s, 3))
                            cntr(2) = cntr(2) + 1
                            trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2)).Tag = trw.Nodes(cntr(0)).Nodes(cntr(1)).Tag.ToString & eol & s
                            trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2)).ImageIndex = 3
                            trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2)).SelectedImageIndex = 4
                            If trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2)).Tag = nodSet.Tag Then nodSet = trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2))
                        End If
                    End If
                End If
            End If
        Next i
        'Navrat na aktualnu poziciu
        Application.DoEvents()
        For i = 0 To myLst.Items.Count - 1
            For Each nod0 In trw.Nodes
                If nod0.Tag = myLst.Items(i).ToString Then nod0.Expand()
                For Each nod1 In nod0.Nodes
                    If nod1.Tag = myLst.Items(i).ToString Then nod1.Expand()
                Next
            Next
        Next i
        Application.DoEvents()
        If nodSet.Tag <> "" Then trw.SelectedNode = nodSet
    Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.Exclamation)
    Finally
        Me.Cursor = Cursors.Default
    End Try

0
这是基于@PrepucioArgentino答案的版本,它在处理集合中的复杂类型以及结构体方面更好。您还可以忽略根据名称(沿整个结构)的属性。但通常情况下,您会想要在那里传递一个空列表。

    public static class TreeNodeHelper
    {

        public static void PrintProperties(TreeNode parentNode, object obj, int indent, TreeView treeView, ICollection<string> ignoredPropertyNames)
        {

            string indentString = new string(' ', indent); // used for debugging


            if (obj == null)
                return;

            var objType = obj.GetType();

            var properties = objType.GetProperties()
                .Where(x => !ignoredPropertyNames.Contains(x.Name))
                .ToList();

            foreach (var property in properties)
            {
                if (property.Name.ToUpper().Contains("DiagMessage".ToUpper()))
                {
                    // TODO rem test code
                }

                var propValue = new object();
                if (property.CanRead)
                    propValue = property.GetValue(obj, null);
                if (IsSimpleType(property.PropertyType))
                {
                    Debug.WriteLine("{0}{1}:", indentString, property.Name);
                    if (parentNode != null)
                    {
                        parentNode.Nodes.Add(property.Name,
                            $"{property.Name}: {(propValue != null ? propValue.ToString() : string.Empty)}");
                    }
                    else
                    {
                        treeView.Nodes.Add(property.Name,
                            $"{property.Name}: {(propValue != null ? propValue.ToString() : string.Empty)}");
                    }

                }

                else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
                {
                    TreeNode collectionRootNode;
                    if (parentNode != null)
                    {
                        collectionRootNode = parentNode.Nodes.Add(property.Name, property.Name);
                    }
                    else
                    {
                        collectionRootNode = treeView.Nodes.Add(property.Name, property.Name);
                    }

                    Debug.WriteLine("{0}{1}:", indentString, property.Name);
                    var enumerable = (IEnumerable)propValue;
                    var count = 0;
                    foreach (var child in enumerable)
                    {
                        count++;

                        if (!IsSimpleType(child.GetType()))
                        {
                            var entryRootNode = collectionRootNode.Nodes.Add($"{property.Name}[{count}]",
                                $"{property.Name}[{count}]");
                            PrintProperties(entryRootNode, child, indent + 2, treeView, ignoredPropertyNames);
                        }
                        else
                        {
                            collectionRootNode.Nodes.Add($"{property.Name}[{count}]", $"{property.Name}[{count}]: {child}");
                        }
                    }
                }
                else
                {

                    TreeNode node;
                    if (parentNode != null)
                    {
                        node = parentNode.Nodes.Add(property.Name, property.Name);
                    }
                    else
                    {
                        node = treeView.Nodes.Add(property.Name, property.Name);
                    }

                    PrintProperties(node, propValue, indent + 2, treeView, ignoredPropertyNames);
                }
            }
        }

        public static bool IsSimpleType(Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                // nullable type, check if the nested type is simple.
                return IsSimpleType(type.GetGenericArguments()[0]);
            }

            return type.IsPrimitive
                   || ((IList)new[]
                   {
                       // Structs that are 'simple types', expand on this when needed.
                       typeof(DateTime),
                       typeof(DateTimeOffset),
                       typeof(TimeSpan),
                       typeof(Guid)
                   }).Contains(type)
                   || type.IsEnum
                   || type == typeof(string)
                   || type == typeof(decimal);
        }

    }


   TreeNodeHelper.PrintProperties(null, firstMotor, 0, treeView1, new List<string>(new[]{ "Creator", "PlcTag", "PlcComponentCreationInfo" }));

0

五年后....

调用 >> PrintProperties(null, yourObject, 0);

   public void PrintProperties(TreeNode parentNode, object obj, int indent)
    {

        if (obj == null) return;
        string indentString = new string(' ', indent);
        Type objType = obj.GetType();
        PropertyInfo[] properties = objType.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            object propValue = new object();
            if (property.CanRead)
                propValue = property.GetValue(obj, null);
            if (IsSimpleType(property.PropertyType))
            {
                //Console.WriteLine("{0}{1}:", indentString, property.Name);
                if (parentNode != null)
                {
                    parentNode.Nodes.Add(property.Name, property.Name +": " + (propValue != null ? propValue.ToString() : string.Empty));
                }
                else
                {
                    treeView.Nodes.Add(property.Name, property.Name + ": " + (propValue != null ? propValue.ToString() : string.Empty));
                }

            }

            else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
            {
                TreeNode node;
                if (parentNode != null)
                {
                    node = parentNode.Nodes.Add(property.Name, property.Name);
                }
                else
                {
                    node = treeView.Nodes.Add(property.Name, property.Name);
                }

                //Console.WriteLine("{0}{1}:", indentString, property.Name);
                IEnumerable enumerable = (IEnumerable)propValue;
                foreach (object child in enumerable)
                    PrintProperties(node, child, indent + 2);
            }
            else
            {

                TreeNode node;
                if (parentNode != null)
                {
                    node = parentNode.Nodes.Add(property.Name, property.Name);
                }
                else
                {
                    node = treeView.Nodes.Add(property.Name, property.Name);
                }

                PrintProperties(node, propValue, indent + 2);
            }
        }
    }

    public static bool IsSimpleType(Type type)
    {
        return
            type.IsValueType ||
            type.IsPrimitive ||
            new Type[]
            {
        typeof(String),
        typeof(Decimal),
        typeof(DateTime),
        typeof(DateTimeOffset),
        typeof(TimeSpan),
        typeof(Guid)
            }.Contains(type) ||
            Convert.GetTypeCode(type) != TypeCode.Object;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接