I'm new to TreeView in WPF: C #, and I'm teaching how to work with dynamically created TreeViewItems . Right now, my program allows the user to create dynamic TreeViewItems and then delete them. For my next step, I would like to program the ability to rename these dynamic elements. Theoretically, this process is pretty straightforward, however I don't know what to do with C # code.
First of all, I would like to outline my renaming process to show exactly where I need help.
- User right click on
TreeViewItem - A
contextMenu crashes (DONE) - The user clicks the "Rename ..." button and the (DONE) window opens.
- The name
TreeViewItem is entered in the textBox (DONE) - If the text in the
textBox matches a header in the mainWindow TreeView , another window opens that allows the user to enter a new name for the TreeViewItem . (Incomplete) - When the name is entered in a new window, the user clicks the button, and the element name changes to everything that was entered in the
textBox . (Incomplete)
Here is my code for step 5, where the program should verify that the entered header exists in the TreeView . In areas where I'm not sure about the correct code, I have pseudo code.
//ENTER - Select TreeViewItem, open enterName window private void button2_Click(object sender, RoutedEventArgs e) { //Query for Window1 var mainWindow = Application.Current.Windows .Cast<Window1>() .FirstOrDefault(window => window is Window1) as Window1; //If(textbox1.text == one of the treeViewItem headers) var newWindow = new EnterCartName(); newWindow.Show(); //else, //MessageBox.Show("Value entered does not match a current cart name"); //this.Close(); }
For step 6, I understand how to add a TreeViewItem with a dynamically created header , but I'm not sure how to get and rename an existing one. This is my code:
//ENTER - Change cart name private void button2_Click(object sender, RoutedEventArgs e) { this.Close(); //close Window //Query for Window1 var mainWindow = Application.Current.Windows .Cast<Window1>() .FirstOrDefault(window => window is Window1) as Window1; //mainWindow.treeViewItem.Header(TVI gotten from previous window) = textBox1.Text; }
I think my main problem with TreeView dynamic assets is that I feel they are invisible to me. How should I work with them if I donβt know what they called, or how to use them correctly. If you know any guides or resources on this topic, feel free to share them.
Many thanks.
My final decision:
//Now Global bool hasFoundMatch; //ENTER - Select cart, open enter name window private void button2_Click(object sender, RoutedEventArgs e) { string input, output; //Query for Window1 var mainWindow = Application.Current.Windows .Cast<Window1>() .FirstOrDefault(window => window is Window1) as Window1; //Get TreeViewItem from mainWindow TreeViewItem renameCart = mainWindow.cartTypes_TI; input = textBox1.Text; output = textBox2.Text; //Check if header exists hasFoundMatch = CheckItemHeader(renameCart.Items, input); //if header exists - set new header //Else - show message box if (hasFoundMatch == true) SetItemHeader(renameCart.Items, input, output); else MessageBox.Show("Value entered does not match a current cart name."); //close window this.Close(); } //Checks to see whether the user entered header exists private bool CheckItemHeader(ItemCollection treeViewItems, string input) { bool hasFoundMatch = false; for (int index = 0; index < treeViewItems.Count; index++) { TreeViewItem item = (TreeViewItem)treeViewItems[index]; string header = item.Header.ToString(); if(header == input) { hasFoundMatch = true; break; } else hasFoundMatch = false; } return hasFoundMatch; } //Changes the selected TVI header private void SetItemHeader(ItemCollection treeViewItems, string input, string output) { for (int index = 0; index < treeViewItems.Count; index++) { TreeViewItem item = (TreeViewItem)treeViewItems[index]; string header = item.Header.ToString(); if (header == input) { item.Header = output; break; } } }