Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#帮助树状视图和复选框内容_C#_.net_Wpf_Treeview - Fatal编程技术网

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

C#帮助树状视图和复选框内容,c#,.net,wpf,treeview,C#,.net,Wpf,Treeview,我真的无法摆脱这个 我在treeview中找到了treeview项目。treeview项包含带有内容的复选框。如何获取内容并将其放入列表中。 现在我得到了这个 foreach (TreeViewItem item in treeView1.Items) { foreach (TreeViewItem childItem in item.Items) { CheckBox che

我真的无法摆脱这个

我在treeview中找到了treeview项目。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);
            }



        }

我不确定我是否正确回答了你的问题,但你可以试试这个

        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);
        }

将您的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);
            }
        }
列表选项列表框=新建列表();
私有列表GetAllItemContainers(TreeViewItems控件)
{
列表所有项=新列表();
对于(int i=0;i
试试这个:

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

//Loop through nodes
列表值=新列表;
foreach(treeView.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);
    }
}
列表值=新列表;
//通过单击按钮或其他控件调用
私有void getTreeValues(对象发送方、事件参数e)
{
foreach(treeView.Nodes中的字符串节点)
{
树节点子节点=(树节点)子节点;
添加(节点)
getNodeValues(儿童);
}
foreach(值中的字符串值)
{
Console.WriteLine(值+“\n”);
}
}
//查找父节点的所有子节点的递归方法。
私有void getNodeValues(树节点父节点)
{
foreach(父节点中的字符串子节点)
{
TreeNode节点=(TreeNode)子节点;
添加(子项);
如果(nodes.nodes.Count!=0)GetNodeValue(子节点);
}
}

这是winforms还是WPF?从treeview猜测。Items,这是WPF。如何将treeview绑定到集合???假设我有Treeview>TreeViewItem Books>TreeViewItem book Items,则不会进入:foreach(TreeViewItem childItem in childItems)添加超过个信息。您的XAML文件是什么样子的?