【问题标题】:C# help treeview and checkbox contentC# 帮助树视图和复选框内容
【发布时间】:2011-04-07 13:56:39
【问题描述】:

我真的无法摆脱这个。

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

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



        }

【问题讨论】:

  • 这是winforms还是WPF?
  • 从 treeView1.Items 猜测,它是 WPF。

标签: c# .net wpf treeview


【解决方案1】:

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

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

【讨论】:

  • 添加更多信息。你的 XAML 文件是什么样子的?
【解决方案2】:

改为将 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);
            }
        }

【讨论】:

  • 如何将树视图绑定到集合???假设我有 Treeview > TreeViewItem Books > TreeViewItem book items
  • 进不去:foreach (TreeViewItem childItem in childItems)
【解决方案3】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 2011-05-27
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多