绑定线性列表到分层树视图

发布于 2024-12-09 05:04:00 字数 732 浏览 1 评论 0原文

class MyItem
{
    public long ID;
    public string Name;
    public long? ParentID;

    public MyItem(long id, string name, long? parent)
    {
        ID = id;
        Name = name;
        ParentID= parent;
    }
}

List<MyItem> myItemList = new List<MyItem>();
myItemList.Add(new MyItem(1, "Item1", null));
myItemList.Add(new MyItem(2, "Item1", 1));
myItemList.Add(new MyItem(3, "Item1", 1));
myItemList.Add(new MyItem(4, "Item1", 5));
myItemList.Add(new MyItem(5, "Item1", null));
myItemList.Add(new MyItem(6, "Item1", 3));
myItemList.Add(new MyItem(7, "Item1", null));

我想使用 XAML 将 myItemList 绑定到 WPF 树视图以获得这样的视图。

1

  |--2

  |--3

     |--6
5

  |--4

7
class MyItem
{
    public long ID;
    public string Name;
    public long? ParentID;

    public MyItem(long id, string name, long? parent)
    {
        ID = id;
        Name = name;
        ParentID= parent;
    }
}

List<MyItem> myItemList = new List<MyItem>();
myItemList.Add(new MyItem(1, "Item1", null));
myItemList.Add(new MyItem(2, "Item1", 1));
myItemList.Add(new MyItem(3, "Item1", 1));
myItemList.Add(new MyItem(4, "Item1", 5));
myItemList.Add(new MyItem(5, "Item1", null));
myItemList.Add(new MyItem(6, "Item1", 3));
myItemList.Add(new MyItem(7, "Item1", null));

I want to bind the myItemList to a WPF treeview with XAML inly to get a view like this.

1

  |--2

  |--3

     |--6
5

  |--4

7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2024-12-16 05:04:00

这是我解决问题的方法。
我需要有 2 个类:

1) LinqToObjectsExtensionMethods:

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

namespace APTClient.Model
{
    static class LinqToObjectsExtensionMethods
    {
        private static IEnumerable<HierarchyNode<TEntity>> CreateHierarchy<TEntity, TProperty>(IEnumerable<TEntity> allItems, TEntity parentItem, Func<TEntity, TProperty> idProperty,
                                                                                           Func<TEntity, TProperty> parentIdProperty, object rootItemId, int maxDepth, int depth)
                                                                                          where TEntity : class
    {
        IEnumerable<TEntity> childs;
        if (rootItemId != null)
        {
            childs = allItems.Where(i => idProperty(i).Equals(rootItemId));
        }
        else
        {
            if (parentItem == null)
            {
                childs = allItems.Where(i => parentIdProperty(i).Equals(default(TProperty)));
            }
            else
            {
                childs = allItems.Where(i => parentIdProperty(i).Equals(idProperty(parentItem)));
            }
        }
        if (childs.Count() > 0)
        {
            depth++;
            if ((depth <= maxDepth) || (maxDepth == 0))
            {
                foreach (var item in childs)
                    yield return
                    new HierarchyNode<TEntity>()
                    {
                        Entity = item,
                        ChildNodes =
                        CreateHierarchy(allItems.AsEnumerable(), item, idProperty, parentIdProperty, null, maxDepth, depth),
                        Depth = depth,
                        Parent = parentItem
                    };
            }
        }
    }
    /// <summary>
    /// LINQ to Objects (IEnumerable) AsHierachy() extension method
    /// </summary>
    /// <typeparam name="TEntity">Entity class</typeparam>
    /// <typeparam name="TProperty">Property of entity class</typeparam>
    /// <param name="allItems">Flat collection of entities</param>
    /// <param name="idProperty">Func delegete to Id/Key of entity</param>
    /// <param name="parentIdProperty">Func delegete to parent Id/Key</param>
    /// <returns>Hierarchical structure of entities</returns>
    public static IEnumerable<HierarchyNode<TEntity>> AsHierarchy<TEntity, TProperty>(this IEnumerable<TEntity> allItems, Func<TEntity, TProperty> idProperty
                                                                                    , Func<TEntity, TProperty> parentIdProperty) where TEntity : class
    {
        return CreateHierarchy(allItems, default(TEntity), idProperty, parentIdProperty, null, 0, 0);
    }
    /// <summary>
    /// LINQ to Objects (IEnumerable) AsHierachy() extension method
    /// </summary>
    /// <typeparam name="TEntity">Entity class</typeparam>
    /// <typeparam name="TProperty">Property of entity class</typeparam>
    /// <param name="allItems">Flat collection of entities</param>
    /// <param name="idProperty">Func delegete to Id/Key of entity</param>
    /// <param name="parentIdProperty">Func delegete to parent Id/Key</param>
    /// <param name="rootItemId">Value of root item Id/Key</param>
    /// <returns>Hierarchical structure of entities</returns>
    public static IEnumerable<HierarchyNode<TEntity>> AsHierarchy<TEntity, TProperty>(this IEnumerable<TEntity> allItems, Func<TEntity, TProperty> idProperty
                                                                                    , Func<TEntity, TProperty> parentIdProperty, object rootItemId) where TEntity : class
    {
        return CreateHierarchy(allItems, default(TEntity), idProperty, parentIdProperty, rootItemId, 0, 0);
    }
    /// <summary>
    /// LINQ to Objects (IEnumerable) AsHierachy() extension method
    /// </summary>
    /// <typeparam name="TEntity">Entity class</typeparam>
    /// <typeparam name="TProperty">Property of entity class</typeparam>
    /// <param name="allItems">Flat collection of entities</param>
    /// <param name="idProperty">Func delegete to Id/Key of entity</param>
    /// <param name="parentIdProperty">Func delegete to parent Id/Key</param>
    /// <param name="rootItemId">Value of root item Id/Key</param>
    /// <param name="maxDepth">Maximum depth of tree</param>
    /// <returns>Hierarchical structure of entities</returns>
    public static IEnumerable<HierarchyNode<TEntity>> AsHierarchy<TEntity, TProperty>(this IEnumerable<TEntity> allItems, Func<TEntity, TProperty> idProperty
                                                                                    , Func<TEntity, TProperty> parentIdProperty, object rootItemId, int maxDepth) where TEntity : class
    {
        return CreateHierarchy(allItems, default(TEntity), idProperty, parentIdProperty, rootItemId, maxDepth, 0);
    }
}}

2) HierarchyNode

using System.Collections.Generic;

namespace APTClient.Model
{
    public class HierarchyNode<T> where T : class
    {
        public T Entity { get; set; }
        public IEnumerable<HierarchyNode<T>> ChildNodes { get; set; }
        public int Depth { get; set; }
        public T Parent { get; set; }
    }
}

我有 List; myItemList;

List<MyItem> myItemList = new List<MyItem>(); 
myItemList.Add(new MyItem(1, "Item1", null)); 
myItemList.Add(new MyItem(2, "Item1", 1)); 
myItemList.Add(new MyItem(3, "Item1", 1)); 
myItemList.Add(new MyItem(4, "Item1", 5)); 
myItemList.Add(new MyItem(5, "Item1", null)); 
myItemList.Add(new MyItem(6, "Item1", 3)); 
myItemList.Add(new MyItem(7, "Item1", null)); 

然后

List<HierarchyNode<MyItem>> = myItemList.AsHierarchy(m => m.ID, m => m.ParentID);

Here is how I solved it.
I need to have 2 classes:

1) LinqToObjectsExtensionMethods:

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

namespace APTClient.Model
{
    static class LinqToObjectsExtensionMethods
    {
        private static IEnumerable<HierarchyNode<TEntity>> CreateHierarchy<TEntity, TProperty>(IEnumerable<TEntity> allItems, TEntity parentItem, Func<TEntity, TProperty> idProperty,
                                                                                           Func<TEntity, TProperty> parentIdProperty, object rootItemId, int maxDepth, int depth)
                                                                                          where TEntity : class
    {
        IEnumerable<TEntity> childs;
        if (rootItemId != null)
        {
            childs = allItems.Where(i => idProperty(i).Equals(rootItemId));
        }
        else
        {
            if (parentItem == null)
            {
                childs = allItems.Where(i => parentIdProperty(i).Equals(default(TProperty)));
            }
            else
            {
                childs = allItems.Where(i => parentIdProperty(i).Equals(idProperty(parentItem)));
            }
        }
        if (childs.Count() > 0)
        {
            depth++;
            if ((depth <= maxDepth) || (maxDepth == 0))
            {
                foreach (var item in childs)
                    yield return
                    new HierarchyNode<TEntity>()
                    {
                        Entity = item,
                        ChildNodes =
                        CreateHierarchy(allItems.AsEnumerable(), item, idProperty, parentIdProperty, null, maxDepth, depth),
                        Depth = depth,
                        Parent = parentItem
                    };
            }
        }
    }
    /// <summary>
    /// LINQ to Objects (IEnumerable) AsHierachy() extension method
    /// </summary>
    /// <typeparam name="TEntity">Entity class</typeparam>
    /// <typeparam name="TProperty">Property of entity class</typeparam>
    /// <param name="allItems">Flat collection of entities</param>
    /// <param name="idProperty">Func delegete to Id/Key of entity</param>
    /// <param name="parentIdProperty">Func delegete to parent Id/Key</param>
    /// <returns>Hierarchical structure of entities</returns>
    public static IEnumerable<HierarchyNode<TEntity>> AsHierarchy<TEntity, TProperty>(this IEnumerable<TEntity> allItems, Func<TEntity, TProperty> idProperty
                                                                                    , Func<TEntity, TProperty> parentIdProperty) where TEntity : class
    {
        return CreateHierarchy(allItems, default(TEntity), idProperty, parentIdProperty, null, 0, 0);
    }
    /// <summary>
    /// LINQ to Objects (IEnumerable) AsHierachy() extension method
    /// </summary>
    /// <typeparam name="TEntity">Entity class</typeparam>
    /// <typeparam name="TProperty">Property of entity class</typeparam>
    /// <param name="allItems">Flat collection of entities</param>
    /// <param name="idProperty">Func delegete to Id/Key of entity</param>
    /// <param name="parentIdProperty">Func delegete to parent Id/Key</param>
    /// <param name="rootItemId">Value of root item Id/Key</param>
    /// <returns>Hierarchical structure of entities</returns>
    public static IEnumerable<HierarchyNode<TEntity>> AsHierarchy<TEntity, TProperty>(this IEnumerable<TEntity> allItems, Func<TEntity, TProperty> idProperty
                                                                                    , Func<TEntity, TProperty> parentIdProperty, object rootItemId) where TEntity : class
    {
        return CreateHierarchy(allItems, default(TEntity), idProperty, parentIdProperty, rootItemId, 0, 0);
    }
    /// <summary>
    /// LINQ to Objects (IEnumerable) AsHierachy() extension method
    /// </summary>
    /// <typeparam name="TEntity">Entity class</typeparam>
    /// <typeparam name="TProperty">Property of entity class</typeparam>
    /// <param name="allItems">Flat collection of entities</param>
    /// <param name="idProperty">Func delegete to Id/Key of entity</param>
    /// <param name="parentIdProperty">Func delegete to parent Id/Key</param>
    /// <param name="rootItemId">Value of root item Id/Key</param>
    /// <param name="maxDepth">Maximum depth of tree</param>
    /// <returns>Hierarchical structure of entities</returns>
    public static IEnumerable<HierarchyNode<TEntity>> AsHierarchy<TEntity, TProperty>(this IEnumerable<TEntity> allItems, Func<TEntity, TProperty> idProperty
                                                                                    , Func<TEntity, TProperty> parentIdProperty, object rootItemId, int maxDepth) where TEntity : class
    {
        return CreateHierarchy(allItems, default(TEntity), idProperty, parentIdProperty, rootItemId, maxDepth, 0);
    }
}}

2) HierarchyNode<T>:

using System.Collections.Generic;

namespace APTClient.Model
{
    public class HierarchyNode<T> where T : class
    {
        public T Entity { get; set; }
        public IEnumerable<HierarchyNode<T>> ChildNodes { get; set; }
        public int Depth { get; set; }
        public T Parent { get; set; }
    }
}

I have the List<MyItem> myItemList;

List<MyItem> myItemList = new List<MyItem>(); 
myItemList.Add(new MyItem(1, "Item1", null)); 
myItemList.Add(new MyItem(2, "Item1", 1)); 
myItemList.Add(new MyItem(3, "Item1", 1)); 
myItemList.Add(new MyItem(4, "Item1", 5)); 
myItemList.Add(new MyItem(5, "Item1", null)); 
myItemList.Add(new MyItem(6, "Item1", 3)); 
myItemList.Add(new MyItem(7, "Item1", null)); 

Then

List<HierarchyNode<MyItem>> = myItemList.AsHierarchy(m => m.ID, m => m.ParentID);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文