/// <summary> /// 界面数据初始化 /// </summary> private void MainWindow_Load() { PropertyNodeItem pni = new PropertyNodeItem() { Icon = str_icon_path + "computer.png", DisplayName = "我的电脑", Name = "我的电脑", isDir = true }; PropertyNodeItem node5 = new PropertyNodeItem() { DisplayName = "Tag No.5", Name = "测试用的无效节点" }; //初始化树 //获得所有硬盘分区 foreach (DriveInfo driveitem in DriveInfo.GetDrives()) { //将硬盘分区名称添加保存到硬盘信息里 list_dick_name.Add(driveitem.Name); string str_type = driveitem.DriveType.ToString(); str_type = drivetype_dict[str_type]; //将分区硬盘信息添加到详细信息里 list_info.Add(new FileNode(false, driveitem.Name, "", str_type, "", str_icon_path + "disk.png")); //将硬盘名称添加到树列表中 string str = driveitem.Name; str = str.Substring(0, str.Length - 1); //添加到树节点中 PropertyNodeItem node_tmp = new PropertyNodeItem() { Icon = str_icon_path + "disk.png", DisplayName = str, Name = driveitem.Name, isDir = true }; //当磁盘访问就绪时才添加树子节点 if (driveitem.IsReady) { node_tmp.Children.Add(node5); } pni.Children.Add(node_tmp); } itemList.Add(pni); this.treeView_Dir.ItemsSource = itemList; //初始化详细信息 dataGrid_Info.DataContext = list_info; //listView_Info.DataContext = list_info; //将返回上一级按钮更改为不可使用 this.btn_last_path.IsEnabled = false; }
/// <summary> /// TreeView的鼠标双击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeView_Dir_MouseDoubleClick(object sender, MouseButtonEventArgs e) { //判断是否选中 if (treeView_Dir.SelectedItem == null) { return; } PropertyNodeItem pro = treeView_Dir.SelectedItem as PropertyNodeItem; //判断是否为文件,是文件则打开文件 if (pro.isDir == false) { System.Diagnostics.Process.Start(pro.Name); return; } //如果双击的“我的电脑”,不操作 if (pro.Name == "我的电脑") { return; } GetChildDirectory(pro.Name); }
/// <summary> /// TreeView的节点展开触发事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeView_Dir_Expanded(object sender, RoutedEventArgs e) { PropertyNodeItem node5 = new PropertyNodeItem() { DisplayName = "Tag No.5", Name = "测试用无效节点" }; TreeViewItem treeitem = e.OriginalSource as TreeViewItem; PropertyNodeItem pro = treeitem.DataContext as PropertyNodeItem; if (pro.Name == "我的电脑") { return; //如果是根节点则退出 } pro.Children.Clear(); //清空子节点,强制重刷新(耗时) //提取出当前路径 string path = pro.Name; //获取当前目录下的所有子目录的文件名称 List <string> list_tree_dir = new FileOperations().GetCurrentDirectoryOnDirname(path); if (list_tree_dir != null) { foreach (string strdir in list_tree_dir) { PropertyNodeItem nodedir = new PropertyNodeItem() { Icon = str_icon_path + "folder.png", DisplayName = strdir, Name = path + strdir + "\\", isDir = true }; nodedir.Children.Add(node5);//给子目录添加一个无效节点,在打开目录时删除该节点 pro.Children.Add(nodedir); } } //获取当前目录下的所有文件的名名称 string[] str_tmp; string str_tmp_icon_name = ""; int index = -1; List <string> list_tree_file = new FileOperations().GetCurrentDirectoryOnFilename(path); if (list_tree_file != null) { foreach (string strdir in list_tree_file) { str_tmp_icon_name = "_blank.png"; str_tmp = strdir.Split('.'); //判断是否有后缀,用'.'拆分出的数组元素为两个就证明有后缀 if (str_tmp.Count() > 1) { index = str_tmp.Count() - 1; //始终将拆分出最后一个字符串作为类型 //获取文件类型对应图标路径 if (Icon_path.ContainsKey(str_tmp[index].ToLower())) //判断是否存在相应的键 { str_tmp_icon_name = Icon_path[str_tmp[index]]; } } str_tmp = null; PropertyNodeItem nodedir = new PropertyNodeItem() { Icon = str_icon_path + str_tmp_icon_name, DisplayName = strdir, Name = path + strdir, isDir = false }; pro.Children.Add(nodedir); } } }