Converting a list into TreeViewItems

こ雲淡風輕ζ 提交于 2019-12-11 22:08:01

问题


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:


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



来源:https://stackoverflow.com/questions/20636610/converting-a-list-into-treeviewitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!