如何把一个树型视图成产生树型视图? [英] How to put a TreeViewItem into a generated TreeViewItem?

查看:120
本文介绍了如何把一个树型视图成产生树型视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#:

TreeViewItem treeItem = null;
        treeItem = new TreeViewItem();
        treeItem.Header = "Audit";


        foreach (cAuditTransactionTypeEntity tt in _Pot)
        {
            char[] charsToTrim = {' ', '\t' };
            treeItem.Items.Add(new TreeViewItem() { Header = tt.TransactionType, Name = tt.TransactionType.Replace(" ", "")});
        }

        ToDoList.Items.Add(treeItem);



XAML:

XAML:

<TreeView x:Name="MyTreeView" HorizontalAlignment="Left" Height="430" Margin="381,409,0,0" VerticalAlignment="Top" Width="616">
        <TreeViewItem Name="ToDoList" Header="To Do List" FontSize="20" IsExpanded="True">  
        </TreeViewItem>
    </TreeView>



我已经加入 TreeViewItems TreeView控件,如上图所示。现在我需要在每一个这些生成的 TreeViewItems 添加更多项目。然而,如图中的XAML(上文),产生的新项目还没有被添加,因此,我无法说出他们引用他们的代码。

I have added TreeViewItems to a TreeView, as shown above. Now I need to add more items under each of these generated TreeViewItems. However, as shown in the XAML (also above), the new generated items have not been added, therefore I couldn't name them to reference them in code.

treeItem.Items.Add(new TreeViewItem() { Header = tt.TransactionType, Name = tt.TransactionType.Replace(" ", "")});



所以,我在我的这部分代码添加一个名字的一代。此外,这种尝试失败了,仍不能引用 TreeViewItems 我产生,作为地名在运行时才会产生。

Therefore, I added a name generation in this part of my code. Furthermore, this attempt failed and can still not reference the TreeViewItems I generated, as the names are only generated at runtime.

有没有解决这个另一种方式,或者是有另一种方式来找到这产生 TreeViewItems 中的代码然后添加新的产品下他们?

Is there another way around this, or is there another way to find each of these generated TreeViewItems in code to then add new Items underneath them?

推荐答案

您只需要保持在树型视图刚刚创建的项目加入到树型视图

You just need to keep a reference to the TreeViewItem that has just created and Add the Items to that treeViewItem

TreeViewItem treeItem = null;
treeItem = new TreeViewItem();
treeItem.Header = "Audit";


foreach (cAuditTransactionTypeEntity tt in _Pot)
{
    TreeViewItem createdTV ;

    char[] charsToTrim = {' ', '\t' };

    //Keep a reference to the created TreeViewItem
    createdTV = new TreeViewItem() { Header = tt.TransactionType, Name = tt.TransactionType.Replace(" ", "")}

    //Create the childs of the createdTreeView

    foreach (cAuditTransactionTypeEntity otherInfo in _OtherList)
    {
        createdTV.Items.Add(new TreeViewItem() { Header = otherInfo.TransactionType, Name = otherInfo.TransactionType.Replace(" ", "")}) ;
    }

    treeItem.Items.Add(createdTV);
}

ToDoList.Items.Add(treeItem);

如果你需要做的是一个特定的孩子的外观和添加项目...

If what you need to do is a look for a specific child and add items to it...

foreach (TreeviewItem objTreeviewItem in ToDoList.Items) 
{
    //change to the desired transaction type
    if ((objTreeviewItem.Header == "TransType")) in your comments
    {
        //add the IDs that correspond to the transaction type
        objTreeviewItem.Items.Add(new TreeViewItem() { Header = child.ID, Name = child.Name}) ;
    }

}

这篇关于如何把一个树型视图成产生树型视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆