Converting a list into TreeViewItems

635 views Asked by At

I have a list of TransactionTypes and I want each of these to be added as Nodes / TreeViewItems on a TreeView, under another TreeViewItem called 'Audit'.

Is there a way to convert each individual item in the list into a new TreeViewItem in a TreeView? And if so, how can it be done most efficiently?

Thanks.

EDIT:

We are using a WPF application and therefore can't use .Nodes / TreeNodes.

1

There are 1 answers

1
Mauricio Gracia Gutierrez On BEST ANSWER

This should work

List<TransactionTypes> lstTrans ;
TreeViewItem auditNode ;

//code that initializes lsTrans
//code that initializes auditNode 

foreach(TransactionTypes tt in lstTrans)
{
    auditNode.Items.Add(new TreeViewItem() { Header = tt.someStringProperty });
}

This is asumming that your TreeViewItem has a header property and is declared similar to this

<TreeView>
    <TreeViewItem Header="initialValue"></TreeViewItem>
</TreeView>

There is probably a more compact way using LINQ but internally is still a loop

Here is a great example of using TreeView in WPF