/// <summary> /// Recursively traverses the tree and adds all checked items, which are files to the FileList /// </summary> /// <param name="parentContainer"></param> private void GetNextContainer(MyTreeViewItem parentContainer) { foreach (var item in parentContainer.Items) { MyTreeViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as MyTreeViewItem; if (currentContainer == null) { continue; } if (currentContainer.IsChecked != null && currentContainer.IsChecked == true && currentContainer.FileType == File) { var addFile = myPresenter.PathAllreadyInList(currentContainer.Tag.ToString(), _fileCollection); if (addFile) { //ListViewSelectedFiles.Items.Add(currentContainer.Tag); var add = new FileItem(); var myInfo = new FileInfo(currentContainer.Tag.ToString()); add.FullPath = currentContainer.Tag.ToString(); add.Name = Path.GetFileNameWithoutExtension(currentContainer.Header.ToString()); double len = myInfo.Length; add.Size = myPresenter.MakeFileSizeToReadableString(len); add.Changed = myInfo.LastWriteTime.ToString(CultureInfo.InvariantCulture); add.Type = Path.GetExtension(add.FullPath).Length > 0 ? Path.GetExtension(add.FullPath).Substring(1) : "Unknown"; _fileCollection.Files.Add(add); } } // If the sub containers of current item is ready, we can directly go to the next // iteration to expand them. GetNextContainer(currentContainer); } }
/// <summary> /// Folder expanded Event handler, fired when user clicks on a folder to expand, /// tree is filled on the fly, only when a folder is expanded we add its /// contents as a new layer /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Folder_Expanded(object sender, RoutedEventArgs e) { var item = (MyTreeViewItem)sender; if (item.Items.Count != 1 || item.Items[0] != _dummyNode) { return; //_dummyNode is needed to know if we can do a folder expansion here, if there is no dummynode, we have allready expanded it, or it isn´t an expandable item } item.Items.Clear(); // remove the dummy Node try { foreach (var s in Directory.GetFiles(item.Tag.ToString())) { // add each file under the current directory to the treeview... if (!myPresenter.IsFileCompressable(s)) { continue; } var subitem = new MyTreeViewItem { Header = s.Substring(s.LastIndexOf("\\", StringComparison.Ordinal) + 1), Tag = s, FileType = File, FontWeight = FontWeights.Normal, IsChecked = false }; item.Items.Add(subitem); } foreach (var s in Directory.GetDirectories(item.Tag.ToString())) { // add each directory under the current directory to the treeview... var subitem = new MyTreeViewItem { Header = s.Substring(s.LastIndexOf("\\", StringComparison.Ordinal) + 1), Tag = s, FileType = Folder, FontWeight = FontWeights.Normal, IsChecked = false }; subitem.Items.Add(_dummyNode);//each new folder gets a dummynode, could be a expandable folder, also a newly added folder has not been expanded yet subitem.Expanded += Folder_Expanded; item.Items.Add(subitem); } } catch (DriveNotFoundException ex1) { MessageBox.Show(ex1.Message); } catch (IOException ex1) { MessageBox.Show(ex1.Message); } catch (UnauthorizedAccessException ex1) { MessageBox.Show(ex1.Message); } }
/// <summary> /// Fill the three with the highest layer ( drive layer ) (initialisation of the FileTree) /// </summary> private void FillTree() { foreach (var s in Directory.GetLogicalDrives()) { MyTreeViewItem item = new MyTreeViewItem { Header = s, Tag = s, FontWeight = FontWeights.Normal, FileType = Drive, IsChecked = false }; item.Items.Add(_dummyNode); item.Expanded += Folder_Expanded; TreeViewDirectory.Items.Add(item); } }
/// <summary> /// This will check all treeviewitems underneath a treeview item, but only files. /// </summary> /// <param name="check">shall the treeview items be checked or not?</param> /// <param name="treeViewItem">The Parent TreeViewItem</param> /// <param name="updateChildFiles">Shall we update the child files or not?</param> private static void SetTreeViewItemChecked(bool check, MyTreeViewItem treeViewItem, bool updateChildFiles) { treeViewItem.IsExpanded = check; treeViewItem.IsSelected = true; treeViewItem.IsChecked = check; //int test = treeViewItem.Items.Count; if (treeViewItem.Items.Count <= 0 || !updateChildFiles) { return; } foreach (object t in treeViewItem.Items) { var currentTreeViewItem = t as MyTreeViewItem; if (currentTreeViewItem != null && currentTreeViewItem.FileType == Folder) { break; // Folders are always added at the end so we can do this } if (currentTreeViewItem != null) { currentTreeViewItem.IsChecked = check; } } }