How do you insert a child node in a TreeView Control in WPF C#?

I have a TreeView control that I have created in XAML in a WPF program using C#.

After adding a couple nodes at the root level, I have written code that loops through the tree structure like so:

        ItemCollection items = treeView1.Items;
        foreach (TreeViewItem n in items)
        {
          ...
        }

Once I find the place in this loop where I want to include a child node, how do I go about inserting a child?

Have a look at,

xaml markup

<Grid>
        <ListView>
            <TreeView Name="tree1">
                <TreeViewItem Header="One">
                </TreeViewItem>
                <TreeViewItem Header="Two">
                </TreeViewItem>
            </TreeView>
        </ListView>
    </Grid>

code

Dictionary<string, List<string>> items = new Dictionary<string, List<string>>()
            {
                {"One",new List<string>() {"A","B","C"}},
                {"Two",new List<string>() {"P","Q"}},
            };

            
            foreach (TreeViewItem tv in tree1.Items)
            {
                foreach (string item in items[tv.Header.ToString()])
                    tv.Items.Add(item);
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.