簡體   English   中英

從路徑列表中填充樹形列表(devexpress)

[英]populate treelist from a list of path (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;
}

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

結果如下:
結果

您可以通過以下代碼運行示例:

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM