I'm new to the TreeView in WPF: C#, and I am learning 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 items. Theoretically, this process is pretty self explanatory, however, I do not know what to do with the C# code.
First of all, I would like to outline my renaming process, to show exactly where I need help.
- User right-clicks on the
TreeViewItem - A
contextMenudrops down (DONE) - User clicks on "Rename..." and a window opens up (DONE)
- Name of
TreeViewItemis entered intotextBox(DONE) - If the text in the
textBoxmatches aheaderin the mainWindow'sTreeView, another window opens up that allows the user to enter the new name for theTreeViewItem. (INCOMPLETE) - When the name is entered in the new window, the user clicks enter and the name of the item is changed to whatever was entered in the
textBox. (INCOMPLETE)
Here is my code for step 5, where the program needs to verify that the header entered exists in the TreeView. In the areas where I am unsure 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 am unsure 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 dynamic TreeView assets is that I feel like they are invisible to me. How am I supposed to do work with them if I don't know what they're called, or how to properly reference them. If you know of any guides or resources on this topic please feel free to share.
Thanks a lot.
My Final Solution:
//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;
}
}
}