WPF中树状图展示
static ListGetMuluList(string path)//获取一级目录并记录到Mulu对象中返回一个mulu类型的list反省集合 { if (Directory.Exists(path)) { List ltml = new List (); string[] arrdir = Directory.GetDirectories(path); for (int i = 0; i < arrdir.Length; i++) { Mulu newml = new Mulu() { title = System.IO.Path.GetFileName(arrdir[i]), path = arrdir[i] }; ltml.Add(newml); SetMuluChildren(newml); } return ltml; } else { return null; } } static void SetMuluChildren(Mulu ml) //递归设置每个mulu对象path目录下的子文件夹记录到children集合中 { string[] arrdir = Directory.GetDirectories(ml.path); ml.numberoffile = Directory.GetFiles(ml.path, "*", SearchOption.AllDirectories).Length; ml.title += " (" + ml.numberoffile + ")"; for (int i = 0; i < arrdir.Length; i++) { Mulu newml = new Mulu() { title = System.IO.Path.GetFileName(arrdir[i]), path = arrdir[i] }; ml.Children.Add(newml); SetMuluChildren(newml); } } public class Mulu { public string title //记录显示的标题 { get; set; } public string path //记录完整的目录 { get; set; } public int numberoffile //记录目录下所有的文件数量 包含子目录 { get; set; } public List Children //记录子目录 { get; set; } public Mulu() { Children = new List (); } }
WPF界面中TreeView
原文:https://www.cnblogs.com/Marth/p/14992484.html