将列表转换为TreeViewItems [英] Converting a list into TreeViewItems
问题描述
我有一个TransactionTypes
列表,我希望将它们中的每个作为Nodes
/TreeViewItems
添加到TreeView的另一个TreeViewItem
下,称为'Audit'
.
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'
.
是否可以将列表中的每个项目转换为TreeView中的新TreeViewItem
?如果是这样,如何才能最有效地完成呢?
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?
谢谢.
我们正在使用WPF应用程序,因此无法使用.Nodes/TreeNodes.
We are using a WPF application and therefore can't use .Nodes / TreeNodes.
推荐答案
这应该有效
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 });
}
这假设您的TreeViewItem
具有header
属性,并且声明与此类似
This is asumming that your TreeViewItem
has a header
property and is declared similar to this
<TreeView>
<TreeViewItem Header="initialValue"></TreeViewItem>
</TreeView>
使用LINQ可能有一种更紧凑的方法,但内部仍然是循环
There is probably a more compact way using LINQ but internally is still a loop
以下是 在WPF中使用TreeView
Here is a great example of using TreeView in WPF
这篇关于将列表转换为TreeViewItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!