【问题标题】:Populate treeview from list in C#从 C# 中的列表填充树视图
【发布时间】:2012-05-23 17:01:17
【问题描述】:

我进行了一些研究,发现了一些与我的问题相近的主题,但没有一个能解决它。

populate treeview from a list of path

http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aspx

http://social.msdn.microsoft.com/Forums/en/winforms/thread/dae1c72a-dd28-4232-9aa4-5b38705c0a97

SharpSvn: Getting repository structure and individual files

我想为我的 SVN 文件夹制作一个存储库浏览器,以便用户可以选择一个文件夹,它将返回到一个文本框。

这是我的实际代码:

    private void sourceTrunkBrowseButton_Click(object sender, EventArgs e)
     {
         using (SvnClient svnClient = new SvnClient())
         {
             Collection<SvnListEventArgs> contents;
             Collection<SvnListEventArgs> contents2;
             List<TreeItem> files = new List<TreeItem>();
             if (svnClient.GetList(new Uri("https://sourcecode/svn/XXXXXX"), out contents))
             {
                 foreach (SvnListEventArgs item in contents)
                 {
                     if (item.Path != "")
                     {
                         files.Add(new TreeItem(item.Path, 0));

                         if (svnClient.GetList(new Uri("https://sourcecode/svn/XXXXX" + item.Path), out contents2) && item.Path != "")
                         {
                             foreach (SvnListEventArgs item2 in contents2)
                             {
                                 if (item2.Path != "")
                                 {
                                     files.Add(new TreeItem(item2.Path, 1));
                                 }
                             }
                         }
                     }
                 }
             }
             svnBrowser_.FillMyTreeView(files);
             svnBrowser_.Show();
         }
     }

还有

  public void FillMyTreeView(List<AutoTrunk.TreeItem> files) 
 {

       // Suppress repainting the TreeView until all the objects have been created.
        svnTreeView.BeginUpdate();

        svnTreeView.Nodes.Clear();
        List<TreeNode> roots = new List<TreeNode>();
         roots.Add(svnTreeView.Nodes.Add("Items"));
         foreach (AutoTrunk.TreeItem item in files)
     {
         if (item.Level == roots.Count) roots.Add(roots[roots.Count - 1].LastNode);
         roots[item.Level].Nodes.Add(item.BrowsePath);
     }

        // Begin repainting the TreeView.
        svnTreeView.EndUpdate();
 }

但我的树看起来像这样:

+---Name1
|   |
|   +------Name2
|   |
|   +------Name3
|   |
|   +------Name5
|   |
|   +------Name6
|
+---Name4

但名称 5 和名称 6 应在名称 4 下

抱歉发了这么长的帖子,谢谢!

【问题讨论】:

  • 在您的第一个foreach (SvnListEventArgs item in contents) 循环中,将第二个if 更改为嵌套在第一个if 中。如果路径为空白,您不希望第二个 if 运行。因为那样你最终只是再次为源中继调用GetList(IE:"https://sourcecode/svn/XXXXXX" + path 变为https://sourcecode/svn/XXXXXX,如果path 是一个空字符串)。
  • 没错,它并没有解决我的问题,但我会改变它。

标签: c# treeview sharpsvn


【解决方案1】:

if(item.Level == roots.Count) 是你的问题我在想...你确定这些物品的等级是正确的吗?例如,如果Name1Name4 是根,那么在遇到第二个根之后会发生什么?这是否给出了预期的结果:

TreeNode root = svnTreeView.Nodes.Add("Items");
TreeNode workingNode = root;
foreach (AutoTrunk.TreeItem item in files)
{
    if (item.Level == 0) 
        workingNode = root.Nodes.Add(item.BrowsePath);
    else
        workingNode.Nodes.Add(item.BrowsePath);
}

只是一个想法。

【讨论】:

    猜你喜欢
    • 2010-11-12
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 2016-01-21
    • 2011-09-18
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多