1

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

我想做的是这样的:

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

这样它就不必加载整个树,而是在每次点击的基础上一次加载几个图层。

4

3 回答 3

1

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

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

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

于 2013-06-22T04:52:15.430 回答
1

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

看同一篇文章中的demo 2

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

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

于 2013-06-21T20:52:38.557 回答
0

感谢所有的投入; 我想我终于弄清楚了如何做我在这里寻找的事情:单击 TreeNode 时发生“System.NullReferenceException”类型的未处理异常

我的代码是:

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

       }



   }`
于 2013-06-22T13:02:53.610 回答