4

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.

  1. User right-clicks on the TreeViewItem
  2. A contextMenu drops down (DONE)
  3. User clicks on "Rename..." and a window opens up (DONE)
  4. Name of TreeViewItem is entered into textBox (DONE)
  5. If the text in the textBox matches a header in the mainWindow's TreeView, another window opens up that allows the user to enter the new name for the TreeViewItem. (INCOMPLETE)
  6. 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;
           }
       }
}
Community
  • 1
  • 1
Eric after dark
  • 1,768
  • 4
  • 31
  • 79

1 Answers1

0

Well setting the header is as easy as:

treeViewItem.Header = textBox1.Text;

Finding it should be similar to finding whether any TreeViewItem.Header matches the text provided in the TextBox:

private void SetItemHeader(ItemCollection treeViewItems, string input, string output)
{
    for (int index = 0; index < treeViewItems.Count; index++)
    {
        TreeViewItem item = (TreeViewItem)treeViewItems[index];
        if (item.Header == input)
        {
            item.Header = output;
            return;
        }
        else if (item.Items.Count > 1) SetItemHeader(item.Items, input, output);
    }
}

UPDATE >>>

You could use this method to check the item headers... if it returns true, you have a match:

private bool CheckItemHeader(ItemCollection treeViewItems, string input)
{
    for (int index = 0; index < treeViewItems.Count; index++)
    {
        TreeViewItem item = (TreeViewItem)treeViewItems[index];
        if (item.Header == input) return true;
        else if (item.Items.Count > 1) return CheckItemHeader(item.Items, input);
    }
    return false;
}

You would use it like:

bool hasFoundMatch = CheckItemHeader(treeView.Items, textInputToMatch);

Then when you have the value to change the header to:

SetItemHeader(treeView.Items, textInputToMatch, textToChangeHeaderTo);
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I see what you're saying, but this way doesn't incorporate step 6, or my second window. Are you suggesting the user enters in both the name of the `TreeViewItem` and the new name of it in the same window? – Eric after dark Jul 29 '13 at 15:17
  • No, I thought that you might be able to edit this method very slightly yourself for your header matching method and then use this one for your header updating. However, I have updated my answer. – Sheridan Jul 29 '13 at 15:24
  • Alright, I just updated my answer with your solution. The check keeps returning as false though (and I am definitely entering the correct name). – Eric after dark Jul 29 '13 at 15:43
  • You're supposed to pass the `ItemCollection` from the `TreeView` into the `CheckItemHeader` method, not just one `TreeViewItem`... unless all of the other `TreeViewItem`s are its children. You're going to have to debug those methods to see what items are being passed in... you're the only one that *can* do that. – Sheridan Jul 29 '13 at 16:01
  • Yes, I'm passing the `TreeViewItem` because I only want to deal with its children. So in that case I should debug? – Eric after dark Jul 29 '13 at 16:59