c#如何在运行时动态添加树节点

时间:2013-06-21 20:30:12

标签: c# treeview

我正在尝试在运行时动态地将节点添加到模拟Windows资源管理器的现有树视图中。我有可用的代码,但由于发生的递归量,检查c:驱动器上的所有文件并创建树需要2-3分钟。

我想做的事情是这样的:

 -NodeClickEvent-
if has children { do nothing }
else { add children and grandchildren to selected node }

这样就不必加载整个树,而是每次点击一次加载几个图层。

3 个答案:

答案 0 :(得分:1)

http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode

查看同一篇文章中的演示2

创建树视图项时,附加虚拟对象 在扩展每个树视图项目时,删除虚拟子项(如果存在)并添加实际子项(如果有的话)。

在上面引用的文章中非常清楚地解释了

答案 1 :(得分:1)

如果您对树视图不是很熟悉,那么您可以先学习本教程http://www.dotnetperls.com/treeview 本教程描述了如何使用treelist。

以下这些链接解释了您正在寻找的内容

http://msdn.microsoft.com/en-us/library/aa984278%28VS.71%29.aspx

http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx

答案 2 :(得分:0)

感谢所有的投入;我想我终于想出了如何做我在这里寻找的东西:An unhandled exception of type 'System.NullReferenceException' occurring when clicking TreeNode

我的代码是:

public Form1()
        {
            InitializeComponent();
            this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
            PopulateTreeView();
        }
 private void PopulateTreeView()
   {
       TreeNode rootNode;

       DirectoryInfo info = new DirectoryInfo(@"c:\\");
       if (info.Exists)
       {
           rootNode = new TreeNode(info.Name);
           rootNode.Tag = info;
           GetDirectories(info.GetDirectories(), rootNode);
           treeView1.Nodes.Add(rootNode);
       }
   }

   private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
   {
       TreeNode aNode;
       //DirectoryInfo[] subSubDirs;
       foreach (DirectoryInfo subDir in subDirs)
       {
           aNode = new TreeNode(subDir.Name, 0, 0);
           aNode.Tag = subDir;
           aNode.ImageKey = "folder";
           /*  try
             {
                   subSubDirs = subDir.GetDirectories();
                   if (subSubDirs.Length != 0)
                   {
                       GetDirectories2(subSubDirs, aNode);
                   }
             }
             catch (System.UnauthorizedAccessException)
             {
                 subSubDirs = new DirectoryInfo[0];
             }*/
           nodeToAddTo.Nodes.Add(aNode);
       }
   }


   void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
   {               
       try
       {
           DirectoryInfo d = new DirectoryInfo(@e.Node.FullPath);
           if (e.Node.Nodes.Count > 0) { /*Do Nothing.*/ } else { GetDirectories(d.GetDirectories(), e.Node); e.Node.Expand(); }

           TreeNode newSelected = e.Node;
           listView1.Items.Clear();
           DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
           ListViewItem.ListViewSubItem[] subItems;

           ListViewItem item = null;

           foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
           {
               item = new ListViewItem(dir.Name, 0);
               subItems = new ListViewItem.ListViewSubItem[]
                {new ListViewItem.ListViewSubItem(item, "Directory"), 
                 new ListViewItem.ListViewSubItem(item, 
                    dir.LastAccessTime.ToShortDateString())};
               item.SubItems.AddRange(subItems);
               listView1.Items.Add(item);
           }
           foreach (FileInfo file in nodeDirInfo.GetFiles())
           {
               item = new ListViewItem(file.Name, 1);
               subItems = new ListViewItem.ListViewSubItem[]
                { new ListViewItem.ListViewSubItem(item, "File"), 
                 new ListViewItem.ListViewSubItem(item, 
                    file.LastAccessTime.ToShortDateString())};

               item.SubItems.AddRange(subItems);
               listView1.Items.Add(item);
           }

           listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
       }
       catch (Exception ex)
       {

            if (ex is System.NullReferenceException || ex is System.UnauthorizedAccessException)
            {
                // Do Nothing.
            }

       }



   }`