简体   繁体   English

将列表转换为TreeViewItems

[英]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' . 我有一个列表TransactionTypes ,我想每个这些被添加为Nodes / TreeViewItems上一个TreeView,在另一TreeViewItem称为'Audit'

Is there a way to convert each individual item in the list into a new TreeViewItem in a TreeView? 有没有一种方法可以将列表中的每个单独的项目转换为TreeViewItem中的新TreeViewItem? 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. 我们正在使用WPF应用程序,因此不能使用.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 假设您的TreeViewItem具有header属性,并且声明与此类似

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

There is probably a more compact way using LINQ but internally is still a loop 使用LINQ可能有一种更紧凑的方法,但内部仍然是一个循环

Here is a great example of using TreeView in WPF 这是在WPF使用TreeView的一个很好的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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