From 9e8ccb59078b66af5b9371a427ee0cb6253c4f0d Mon Sep 17 00:00:00 2001 From: chenliang624 Date: Sun, 26 Sep 2021 16:00:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E8=8E=B7=E5=8F=96TreeNode?= =?UTF-8?q?=E7=9A=84=E4=B8=89=E7=A7=8D=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Static/UControl.cs | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/SunnyUI/Static/UControl.cs b/SunnyUI/Static/UControl.cs index 75a99e8d..6c9c046e 100644 --- a/SunnyUI/Static/UControl.cs +++ b/SunnyUI/Static/UControl.cs @@ -421,5 +421,44 @@ namespace Sunny.UI propertyInfo.SetValue(control, true, null); } } + + /// + /// 获取此TreeNode的选中状态 + /// + /// + /// 全选状态(Checked),半选状态(Indeterminate),未选状态(Unchecked) + public static CheckState CheckState(this TreeNode node) + { + if (GetChildNodeCheckedState(node)) + { + return System.Windows.Forms.CheckState.Indeterminate; + } + + return node.Checked ? System.Windows.Forms.CheckState.Checked : System.Windows.Forms.CheckState.Unchecked; + } + + private static bool GetChildNodeCheckedState(TreeNode node) + { + + if (node.Nodes.Count > 0) + { + var count = node.Nodes.Cast().Where(n => n.Checked).ToList().Count; + if (count > 0 && count < node.Nodes.Count) + { + return true; + } + else + { + foreach (TreeNode nd in node.Nodes) + { + return GetChildNodeCheckedState(nd); + } + } + + + } + return false; + } + } } \ No newline at end of file