简体   繁体   中英

Converting a list into TreeViewItems

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM