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?
You just need to keep a reference to the
TreeViewItem
that has just created and Add the Items to that treeViewItemIf what you need to do is a look for a specific child and add items to it...