从路径列表填充树形图(devexpress)

时间:2016-12-23 04:31:48

标签: c# devexpress

我正在尝试从文件夹路径列表中填充TreeList,例如:

C:\WINDOWS\addins
C:\WINDOWS\AppPatch
C:\WINDOWS\AppPatch\MUI
C:\WINDOWS\AppPatch\MUI\040C
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409

和输出我想这样

  ├───addins
  ├───AppPatch
  │   └───MUI
  │       └───040C
  ├───Microsoft.NET
  │   └───Framework
  │       └───v2.0.50727
  │           └───MUI
  │               └───0409

注意:我正在使用Devexpress TreeList

这是我的代码:

private void PopulateTreeList(TreeList treeList, IEnumerable<string> paths, char pathSeparator)
{
    TreeListNode lastNode = null;
    string subPathAgg;
    foreach (string path in paths)
    {
        subPathAgg = string.Empty;
        foreach (string subPath in path.Split(pathSeparator))
        {
            subPathAgg += subPath + pathSeparator;
            TreeListNode nodes = treeList.FindNode((node) => { return node[""].ToString() == subPath; });
            if (nodes == null)
                lastNode = treeList.AppendNode(new object[] { subPath }, lastNode);
            else
            {                   
               if(subPathAgg== GetFullPath(nodes, "\\"))

                   lastNode = nodes;
               else
                   lastNode = treeList.AppendNode(new object[] { subPath }, lastNode);
            }                    
        }
        lastNode = null; 
    }
}

private string GetFullPath(TreeListNode node, string pathSeparator)
{
    if (node == null) return "";
    string result = "";
    while (node != null)
    {
        result = node.GetDisplayText(0) + pathSeparator + result;
        node = node.ParentNode;
    }
    return result;
}

1 个答案:

答案 0 :(得分:0)

TreeList.FindNode在所有节点中执行搜索。您可以使用TreeListNodes.FirstOrDefault方法仅在当前节点的子节点中进行搜索 这是一个例子:

private void PopulateTreeList(TreeList treeList, IEnumerable<string> paths, char pathSeparator)
{
    foreach (string path in paths)
    {
        TreeListNode parentNode = null;

        foreach (string folder in path.Split(pathSeparator))
        {
            var nodes = parentNode?.Nodes ?? treeList.Nodes;

            var node = nodes.FirstOrDefault(item => item[0].Equals(folder));

            if (node == null)
                node = treeList.AppendNode(new object[] { folder }, parentNode);

            parentNode = node;
        }
    }
}

结果如下:
Result

您可以通过以下代码运行示例:

var paths = new[]
{
    @"C:\WINDOWS\addins",
    @"C:\WINDOWS\AppPatch",
    @"C:\WINDOWS\AppPatch\MUI",
    @"C:\WINDOWS\AppPatch\MUI\040C",
    @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
    @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
    @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
};

var treeList = new TreeList() { Size = new Size(400, 250) };
var column = treeList.Columns.Add();
column.VisibleIndex = 0;

PopulateTreeList(treeList, paths, Path.DirectorySeparatorChar);

treeList.ExpandAll();

var form = new Form() { Size = new Size(500, 350) };
form.Controls.Add(treeList);
form.Show();