public void LoadData(List<Group> groups) { listView.Items.Clear(); foreach (TreeViewItem item in treeView.Items) { item.UnregisterName(item.Header.ToString()); } treeView.Items.Clear(); foreach (Group group in groups) { TreeViewItem treeViewItem = new TreeViewItem() { Header = group.Name, Tag = group }; treeView.Items.Add(treeViewItem); treeViewItem.RegisterName(group.Name, treeViewItem); var persons = group.Persons; foreach (Person person in persons) { ListViewItem listViewItem = new ListViewItem() { Content = person, Tag = person }; listView.Items.Add(listViewItem); TreeViewItem i = treeView.FindName(group.Name) as TreeViewItem; TreeViewItem treeViewSubItem = new TreeViewItem() { Header = person.Name, Tag = person }; i.Items.Add(treeViewSubItem); } treeViewItem.IsExpanded = true; } }
/// <summary> /// this is the function that maps the directories from the computer recursively /// </summary> /// <param name="directory"></param> /// <param name="parentNode"></param> /// <param name="tree"></param> /// <param name="names"></param> /// <returns></returns> public TreeView getDirectories(DirectoryInfo directory, string parentNode, TreeView tree, ref List<string> names) { // TODO : Maybe the name of the treeviewitems should be the fullpath? that ensures its unique and we could later // use the names (full paths) in order to find the file quickly for TFS stuff // FIXED : have to pass the name of the parent node instead of using directory.Name because of the unique naming of the tree // elements. by passing the unique name we can find the correct parent node TreeViewItem parent = tree.FindName(parentNode) as TreeViewItem; // gets down to last directory DirectoryInfo[] dir = directory.GetDirectories(); if (dir.Length > 0) { foreach (DirectoryInfo subDir in dir) { // im just skipping over the git stuff for now. we can figure out how to deal with it later if (!subDir.Name.Equals(".git")) { string header = subDir.Name; string name = header.Split('.')[0]; // passes the name to a function that addes numbers to the end of the name to // ensure the name is unique name = uniqueName(name, ref names); TreeViewItem item = new TreeViewItem() { Header = header, Name = name }; parent.Items.Add(item); item.RegisterName(name, item); names.Add(name); getDirectories(subDir,name, tree, ref names); } } } //map files to current level of tree FileInfo[] files = directory.GetFiles(); if (files.Length > 0) { foreach (FileInfo file in files) { string header = file.Name; // this is more git stuff that we dont need to put in here but we might need to add more error stuff to catch weird // names and such if(header.Contains("git")) { header = header.Replace(".", "NotNeeded"); } string name = header.Split('.')[0]; // passes the name to a function that addes numbers to the end of the name to // ensure the name is unique name = uniqueName(name, ref names); TreeViewItem item = new TreeViewItem() { Header = header, Name = name }; parent.Items.Add(item); item.RegisterName(name, item); names.Add(name); } } return tree; }
/// <summary> /// populates a tree view from an xml file... calls a recursive function to finish it /// </summary> private void populateTreeview() { // this try catch will try to populate the tree from the xml file but // if there is no file then it will populate the tree based on the project structure from the computer try { //load the tree that was serialized previously XmlDocument xDoc = new XmlDocument(); xDoc.Load("xmlSerializerd.xml"); //dont think this is needed since this will be run on load treeView1.Items.Clear(); //adds the root of the tree TreeViewItem root = new TreeViewItem() { Header = xDoc.DocumentElement.Name, Name = xDoc.DocumentElement.Name }; treeView1.Items.Add(root); //recursive function to populate the rest of the tree addTreeNode(xDoc.DocumentElement, root); } catch (XmlException xExc) //Exception is thrown is there is an error in the Xml { MessageBox.Show(xExc.Message); } // if there is no file then it will load it from the computer catch (FileNotFoundException) { DirectoryInfo di = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory()); // TODO : this might have to change to a better way to find the project root. di = di.Parent.Parent.Parent; List<string> registeredNames = new List<string>(); //sets the root of the tree var item = new TreeViewItem() { Header = di.Name, Name = di.Name }; treeView1.Items.Add(item); item.RegisterName(di.Name, item); registeredNames.Add(di.Name); MessageBox.Show("I couldnt find the file so im mimicing the structure from the computer"); treeView1 = getDirectories(di, di.Name, treeView1, ref registeredNames); } catch (Exception ex) //General exception { MessageBox.Show(ex.Message); } }
private void addDataToTree(TreeView treeView1, List<dynamic> actualList, List<dynamic> resultList, DBModule.TypeOfDepth depth, String value, int count) { // if (sendQuery != null) // { switch (depth) { case DBModule.TypeOfDepth.Section: { int i = count; foreach (DBModule.Section sectionName in resultList) { if (!isAdded(actualList, sectionName, depth)) { TreeViewItem mainItem = new TreeViewItem() { Header = "Section: " + sectionName.SectionName, Name = "item" + i }; treeView1.Items.Add(mainItem); mainItem.RegisterName("regName" + i, mainItem); i++; } } break; } case DBModule.TypeOfDepth.ForumThread: { int i = count; foreach (ForumThread forumThread in resultList) { if (!isAdded(actualList, forumThread, depth)) { if (!dict.ContainsKey(forumThread.SectionName)) { TreeViewItem mainItem = new TreeViewItem() { Header = "Section: " + forumThread.SectionName, Name = "item" + i }; treeView1.Items.Add(mainItem); dict.Add(forumThread.SectionName, "regName" + i); mainItem.RegisterName("regName" + i, mainItem); TreeViewItem subItem = new TreeViewItem() { Header = "Thread: " + forumThread.ThreadName, Name = "subItem" + i }; mainItem.Items.Add(subItem); subItem.RegisterName("regSubName" + i, subItem); } else { TreeViewItem searchItem = treeView1.FindName(dict[forumThread.SectionName]) as TreeViewItem; TreeViewItem subItem = new TreeViewItem() { Header = "Thread: " + forumThread.ThreadName, Name = "subItem" + i }; searchItem.Items.Add(subItem); subItem.RegisterName("regSubName" + i, subItem); } i++; } } break; } case DBModule.TypeOfDepth.Post: { int i = count; foreach (Post post in resultList) { if (!isAdded(actualList, post, depth)) { if (!dictSections.ContainsKey(post.SectionTitle)) { TreeViewItem mainItem = new TreeViewItem() { Header = "Section: " + post.SectionTitle, Name = "item" + i }; treeView1.Items.Add(mainItem); dictSections.Add(post.SectionTitle, "regName" + i); mainItem.RegisterName("regName" + i, mainItem); if (!dictThreads.ContainsKey(post.TopicTitle)) { TreeViewItem subItem = new TreeViewItem() { Header = "Thread: " + post.TopicTitle, Name = "subItem" + i }; mainItem.Items.Add(subItem); dictThreads.Add(post.TopicTitle, "regSubName" + i); subItem.RegisterName("regSubName" + i, subItem); TreeViewItem postItem = new TreeViewItem() { Header = "Post: " + ChangeName(post.Content), Name = "postItem" + i }; subItem.Items.Add(postItem); postItem.RegisterName("regPostName" + i, postItem); } else { TreeViewItem searchThreadItem = treeView1.FindName(dictThreads[post.TopicTitle]) as TreeViewItem; TreeViewItem postItem = new TreeViewItem() { Header = "Post: " + ChangeName(post.Content), Name = "postItem" + i }; searchThreadItem.Items.Add(postItem); postItem.RegisterName("regPostName" + i, postItem); } } else { TreeViewItem searchItem = treeView1.FindName(dictSections[post.SectionTitle]) as TreeViewItem; if (!dictThreads.ContainsKey(post.TopicTitle)) { TreeViewItem subItem = new TreeViewItem() { Header = "Thread: " + post.TopicTitle, Name = "subItem" + i }; searchItem.Items.Add(subItem); subItem.RegisterName("regSubName" + i, subItem); TreeViewItem postItem = new TreeViewItem() { Header = "Post: " + ChangeName(post.Content), Name = "postItem" + i }; subItem.Items.Add(postItem); postItem.RegisterName("regPostName" + i, postItem); } else { TreeViewItem searchThreadItem = treeView1.FindName(dictThreads[post.TopicTitle]) as TreeViewItem; TreeViewItem postItem = new TreeViewItem() { Header = "Post: " + ChangeName(post.Content), Name = "postItem" + i }; searchThreadItem.Items.Add(postItem); postItem.RegisterName("regPostName" + i, postItem); } } i++; } } break; } } }