C#帮助树视图和复选框内容

时间:2011-04-07 13:56:39

标签: c# .net wpf treeview

我真的离不开这个。

我在树视图中获得了树视图项。树视图项包含带有内容的复选框。如何获取内容并将其放入列表中。 目前我得到了这个

        foreach (TreeViewItem item in treeView1.Items)
        {


            foreach (TreeViewItem childItem in item.Items)
            {


                CheckBox checkBoxTemp = childItem.Header as CheckBox;

                if (checkBoxTemp == null) continue;

                optieListBox.Items.Add(checkBoxTemp.Content);
            }



        }

3 个答案:

答案 0 :(得分:0)

我不确定我是否正确地提出了您的问题,但您可以尝试一下。

        foreach (TreeViewItem childItem in item.Items)
        {
            CheckBox cbx = null;
            //finds first checkbox
            foreach(object child in childItem.Items){
                cbx = child as CheckBox;
                if (cbx != null) break;
            }

            ctrList.Items.Add(cbx.Content);
        }

答案 1 :(得分:0)

将TreeView绑定到集合中。这样您就不必操纵UI组件来访问数据,您将直接访问数据。

另一种方法是通过递归:在类级别声明optieListBox列表并调用GetContainers()方法作为入口点调用。 optieListBox列表应该为您提供树视图中所有已检查项目的内容列表。

List<string> optieListBox = new List<string>();

        private List<TreeViewItem> GetAllItemContainers(TreeViewItem itemsControl)
        {
            List<TreeViewItem> allItems = new List<TreeViewItem>();
            for (int i = 0; i < itemsControl.Items.Count; i++)
            {
                // try to get the item Container  
                TreeViewItem childItemContainer = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
                // the item container maybe null if it is still not generated from the runtime  
                if (childItemContainer != null)
                {
                    allItems.Add(childItemContainer);
                    List<TreeViewItem> childItems = GetAllItemContainers(childItemContainer);
                    foreach (TreeViewItem childItem in childItems)
                    {
                        CheckBox checkBoxTemp = childItem.Header as CheckBox;

                        if (checkBoxTemp != null)
                            optieListBox.Items.Add(checkBoxTemp.Content);

                        allItems.Add(childItem);
                    }
                }
            }
            return allItems;
        }

        private void GetContainers()
        {
            // gets all nodes from the TreeView  
            List<TreeViewItem> allTreeContainers = GetAllItemContainers(this.objTreeView);
            // gets all nodes (recursively) for the first node  
            TreeViewItem firstNode = this.objTreeView.ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;
            if (firstNode != null)
            {
                List<TreeViewItem> firstNodeContainers = GetAllItemContainers(firstNode);
            }
        }

答案 2 :(得分:0)

试试这个:

List<string> values = new List<string>;
foreach (string node in treeView.Nodes)
{
    values.Add(node);
}

//Loop through nodes

此外,如果树视图的节点有子节点(节点),请尝试改为:

List<string> values = new List<string>;

//Called by a button click or another control
private void getTreeValues(Object sender, EventArgs e)
{
    foreach (string node in treeView.Nodes)
    {
        TreeNode child = (TreeNode)child;
        values.Add(node)
        getNodeValues(child);
    }
    foreach (string value in values)
    {
        Console.WriteLine(value + "\n");
    }
}

//Recursive method which finds all children of parent node.
private void getNodeValues(TreeNode parent)
{
    foreach (string child in parent.Nodes)
    {
        TreeNode node = (TreeNode)child;
        values.Add(child);
        if (nodes.Nodes.Count != 0) getNodeValues(child);
    }
}