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