如何检查或取消所有子节点的树视图 [英] How to Check or Uncheck All Child Nodes in TreeView

查看:169
本文介绍了如何检查或取消所有子节点的树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,取消在我的应用程序,但它不是运作良好。如果我要取消选择的文件夹,将取消。但是,子文件夹内的文件夹将保持选中状态(选中)。

I have a Deselect button in my application, but its not working well. If I am going to deselect the folder it will deselect. But the folder within a subfolder will remain selected(checked).

任何帮助,这个问题将不胜感激。

Any help with this issue would be appreciated.

在这里输入的形象描述

推荐答案

您应该的找到包括后代的所有节点,然后将选中=假

例如,你可以使用这个扩展方法来获得树或节点的后代的所有后代节点:

For example you can use this extension method to get all descendant nodes of tree or descendants of a node:

using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;

public static class Extensions
{
    public static List<TreeNode> Descendants(this TreeView tree)
    {
        var nodes = tree.Nodes.Cast<TreeNode>();
        return nodes.SelectMany(x => x.Descendants()).Concat(nodes).ToList();
    }

    public static List<TreeNode> Descendants(this TreeNode node)
    {
        var nodes = node.Nodes.Cast<TreeNode>().ToList();
        return nodes.SelectMany(x => Descendants(x)).Concat(nodes).ToList();
    }
}



然后你可以使用上面的树或节点的方法取消选中树的所有子孙节点或取消节点的所有子节点:

Then you can use above methods on tree or a node to uncheck all descendant nodes of tree or uncheck all descendant nodes of a node:

树的取消选中子节点:

this.treeView1.Descendants().Where(x => x.Checked).ToList()
              .ForEach(x => { x.Checked = false; });



节点的取消选中子节点:

例如节点0:

this.treeView1.Nodes[0].Descendants().Where(x => x.Checked).ToList()
              .ForEach(x => { x.Checked = false; });



不要忘了添加使用System.Linq的;

这篇关于如何检查或取消所有子节点的树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆