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

本文关键字:复选框 视图 帮助 | 更新日期: 2023-09-27 17:49:36

我真的无法摆脱这个。

我在树视图中有树视图项。树视图项包含带有内容的复选框。我如何得到的内容,并把它放在一个列表。目前我得到了这个

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

        }

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

我不确定我是否正确理解你的问题,但你可以试试这个

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

试试这个:

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