private void TreeItemInvoked(TreeView sender, TreeViewItemInvokedEventArgs arguments) { //Debug.WriteLine("Hey, do the thing."); TreeCodeCollection item = arguments.InvokedItem as TreeCodeCollection; if (item.data.NAICSCode == "000000") { ViewModel.LimitDisplayNAICSCode(""); } else { ViewModel.LimitDisplayNAICSCode(item.data.NAICSCode); } }
//Limit the codes displayed in the tree function by making the user's input code the new root private void LimitTreeView(string codeInput) { TreeObjectCollection <CompanyCode> root = CompanyCodeTree; TreeObjectCollection <CompanyCode> oldRoot = root; if (codeInput.Length > 1 && codeInput.Length <= 6) { while (root.data.NAICSCode != codeInput) { for (int i = 0; i < root.children.Count; i++) { if (codeInput != root.children[i].data.NAICSCode) { if (codeInput.StartsWith(root.children[i].data.NAICSCode)) { root = root.children[i]; break; } } else { //Debug.WriteLine("codeInput: " + codeInput + " = " + root.children[i].data.NAICSCode); //Debug.WriteLine("It has " + root.children[i].observableChildren.Count + " children."); root = root.children[i]; break; } } if (root == oldRoot) { //Debug.WriteLine("Root == oldRoot."); break; } else { oldRoot = root; } } } TreeCodeCollection rootToAdd = (TreeCodeCollection)root; ViewTreeModel.Clear(); ViewTreeModel.Add(rootToAdd); //Debug.WriteLine("The children in the view model: " + ViewTreeModel[0].observableChildren.Count); SetLabels(rootToAdd.data); }
//Fill out the tree structure private void ConstructTree() { //Arbitrary root for the tree, it is displayed, and can change when user inputs into a text field TreeCodeCollection root = new TreeCodeCollection(new CompanyCode("000000", "Root")); //This 2D List contains several rows of tree organized by the length of their NAICS code List <TreeCodeCollection>[] TwoDList = { new List <TreeCodeCollection>(), new List <TreeCodeCollection>(), new List <TreeCodeCollection>(), new List <TreeCodeCollection>() }; for (int i = 0; i < ViewModel.CompanyCodeList.Count; i++) { if (ViewModel.CompanyCodeList[i].NAICSCode.Length == 2) { TwoDList[0].Add(new TreeCodeCollection(ViewModel.CompanyCodeList[i], root)); root.children.Add(TwoDList[0][TwoDList[0].Count - 1]); root.observableChildren.Add(TwoDList[0][TwoDList[0].Count - 1]); Debug.WriteLine("Added Child: " + TwoDList[0][TwoDList[0].Count - 1].data.NAICSCode); } else if (ViewModel.CompanyCodeList[i].NAICSCode.Length == 3) { TwoDList[1].Add(new TreeCodeCollection(ViewModel.CompanyCodeList[i])); } else if (ViewModel.CompanyCodeList[i].NAICSCode.Length == 4) { TwoDList[2].Add(new TreeCodeCollection(ViewModel.CompanyCodeList[i])); } else if (ViewModel.CompanyCodeList[i].NAICSCode.Length >= 5) { TwoDList[3].Add(new TreeCodeCollection(ViewModel.CompanyCodeList[i])); } } //Starting from the bottom of the 2D List, the 5+ digit codes, point the trees to their parents and their parents to them for (int i = 3; i > 0; i--) { int tempCount = TwoDList[i].Count; TreeObjectCollection <CompanyCode> lastParent = root; int lastParentIndex = 0; for (int j = 0; j < TwoDList[i].Count; j++) { if (TwoDList[i][j].data.NAICSCode.StartsWith(lastParent.data.NAICSCode)) { TwoDList[i][j].parent = lastParent; TwoDList[i][j].parent.children.Add(TwoDList[i][j]); TwoDList[i][j].parent.observableChildren.Add(TwoDList[i][j]); //TwoDList[i].RemoveAt(j); //yield return null; } else { for (int k = 0; k < TwoDList[i - 1].Count; k++) { if (TwoDList[i][j].data.NAICSCode.StartsWith(TwoDList[i - 1][k].data.NAICSCode)) { TwoDList[i][j].parent = TwoDList[i - 1][k]; TwoDList[i][j].parent.children.Add(TwoDList[i][j]); TwoDList[i][j].parent.observableChildren.Add(TwoDList[i][j]); lastParent = TwoDList[i][j].parent; lastParentIndex = k + 1; } } } } } //Add this tree to the ViewTreeModel for display ViewTreeModel.Add(root); //Copy it to the CompanyCodeTree for storage CompanyCodeTree = root; }