How to put a TreeViewItem into a generated TreeViewItem?

233 views Asked by At

C#:

TreeViewItem treeItem = null;
        treeItem = new TreeViewItem();
        treeItem.Header = "Audit";


        foreach (cAuditTransactionTypeEntity tt in _Pot)
        {
            char[] charsToTrim = {' ', '\t' };
            treeItem.Items.Add(new TreeViewItem() { Header = tt.TransactionType, Name = tt.TransactionType.Replace(" ", "")});
        }

        ToDoList.Items.Add(treeItem);

XAML:

<TreeView x:Name="MyTreeView" HorizontalAlignment="Left" Height="430" Margin="381,409,0,0" VerticalAlignment="Top" Width="616">
        <TreeViewItem Name="ToDoList" Header="To Do List" FontSize="20" IsExpanded="True">  
        </TreeViewItem>
    </TreeView>

I have added TreeViewItems to a TreeView, as shown above. Now I need to add more items under each of these generated TreeViewItems. However, as shown in the XAML (also above), the new generated items have not been added, therefore I couldn't name them to reference them in code.

treeItem.Items.Add(new TreeViewItem() { Header = tt.TransactionType, Name = tt.TransactionType.Replace(" ", "")});

Therefore, I added a name generation in this part of my code. Furthermore, this attempt failed and can still not reference the TreeViewItems I generated, as the names are only generated at runtime.

Is there another way around this, or is there another way to find each of these generated TreeViewItems in code to then add new Items underneath them?

2

There are 2 answers

2
Mauricio Gracia Gutierrez On BEST ANSWER

You just need to keep a reference to the TreeViewItem that has just created and Add the Items to that treeViewItem

TreeViewItem treeItem = null;
treeItem = new TreeViewItem();
treeItem.Header = "Audit";


foreach (cAuditTransactionTypeEntity tt in _Pot)
{
    TreeViewItem createdTV ;

    char[] charsToTrim = {' ', '\t' };

    //Keep a reference to the created TreeViewItem
    createdTV = new TreeViewItem() { Header = tt.TransactionType, Name = tt.TransactionType.Replace(" ", "")}

    //Create the childs of the createdTreeView

    foreach (cAuditTransactionTypeEntity otherInfo in _OtherList)
    {
        createdTV.Items.Add(new TreeViewItem() { Header = otherInfo.TransactionType, Name = otherInfo.TransactionType.Replace(" ", "")}) ;
    }

    treeItem.Items.Add(createdTV);
}

ToDoList.Items.Add(treeItem);

If what you need to do is a look for a specific child and add items to it...

foreach (TreeviewItem objTreeviewItem in ToDoList.Items) 
{
    //change to the desired transaction type
    if ((objTreeviewItem.Header == "TransType")) in your comments
    {
        //add the IDs that correspond to the transaction type
        objTreeviewItem.Items.Add(new TreeViewItem() { Header = child.ID, Name = child.Name}) ;
    }

}
2
nvoigt On

I strongly suggest you use MVVM and a hierarchical data template instead of doing this in code. If you don't use MVVM, you will fight WPF all the way.

There are good tutorials all over the net, one can be found here.