如何在C#中将TreeView项显示为ListView项

本文关键字:显示 ListView TreeView 中将 | 更新日期: 2023-09-27 18:27:19

我在windows应用程序中有树视图,在树视图中有复选框,我有一些"父节点"和一些"子节点",我想单击"全选"answers"全选"按钮一次检查和取消检查父节点和子节点。。。我该怎么做?

现在,如果我单击特定的父节点以及子文件夹,它应该在列表视图中显示它!

在此处输入代码

namespace howto_treeview_select_subtree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            trvMeals.CheckBoxes = true;
            foreach (TreeNode node in trvMeals.Nodes)
            {
                ExpandNode(node);
            }

            trvMeals.SelectedNode = trvMeals.Nodes[0];
            foreach (TreeNode node in trvMeals.Nodes)
            {
                node.Checked = true;
            }
        }
        private void ExpandNode(TreeNode node)
        {
            node.EnsureVisible();
            foreach (TreeNode child in node.Nodes)
            {
                ExpandNode(child);
            }
        }

        private void trvMeals_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;
            bool is_checked = node.Checked;
            foreach (TreeNode child in node.Nodes)
            {
                child.Checked = is_checked;
            }
            trvMeals.SelectedNode = node;
        }
        private void trvMeals_AfterSelect(object sender, TreeViewEventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in trvMeals.Nodes)
            {
                node.Checked = true;
                CheckChildren(node, true);
            }
            //foreach (TreeNode node in trvMeals.Nodes)
            //{
            //    node.Checked = true;
            //}
        }
        private void button2_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in trvMeals.Nodes)
            {
                node.Checked = false;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
           listView1.Text = (listView1 + "'n") + trvMeals.SelectedNode.Text;
            //foreach (TreeNode node in trvMeals.Nodes)
            //{
            //    RcTxt.Text = RcTxt.Text + trvMeals.SelectedNode.Text;
            //}
        }
        private void CheckChildren(TreeNode rootNode, bool isChecked)
        {
            foreach (TreeNode node in rootNode.Nodes)
            {
                CheckChildren(node, isChecked);
                node.Checked = isChecked;
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Clear();
        }
    }
}

如何在C#中将TreeView项显示为ListView项

一种方法是为TreeViewAfterSelect事件创建事件处理程序。

像这样的

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
    //grab the listView object and sender as TreeView. 
    //grab whatever you need from the TreeView
    //Put whatever you need in the listView
}

要执行检查和取消检查,请为TreeView事件AfterCheck创建另一个事件处理程序。

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) {
    TreeView view = sender as TreeView;         
    //Access the treeview nodes, check/uncheck them
}

编辑:

扩展代码:

private void updateChildrenNodes(TreeNode node, bool isChecked) //recursive call
{
    node.Checked = isChecked;
    if (node.Nodes.Count > 0) //has children, do recursive call            
        foreach (TreeNode childNode in node.Nodes)
            updateChildrenNodes(childNode, isChecked);            
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    TreeView view = sender as TreeView;
    TreeNode node = view.SelectedNode;
    bool isChecked = node.Checked;
    updateChildrenNodes(node, isChecked);
}
List<TreeNode> checkedNodes = new List<TreeNode>();
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    TreeView treeView = sender as TreeView;
    listView1.Clear(); //reset all the nodes
    nodes.Clear(); //clears the list
    //grab whatever you need from the TreeView, check if the TreeNode is checked
    //do the same trick by recursive call to put the checked nodes to checkedNodes list
    foreach (TreeNode checkedNode in checkedNodes)
    {
        //do something, use this info to put in listView1
    }            
}

我已经扩展了代码,以便您有更好的想法。希望它能有所帮助。

注意:我还没有彻底检查代码,因为这可能需要一段时间,但我相信这应该足以让你获得必要的工具和基本想法。