From 76b13321e6421e44f371ae38b9dc9cd2c04140e0 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 6 Nov 2024 14:33:31 +0800 Subject: [PATCH] =?UTF-8?q?*=20UControl:=20UITreeView=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=B8=AA=E5=88=B7=E6=96=B0=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E7=9A=84=E6=89=A9=E5=B1=95=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Common/UControl.cs | 39 ++++++++++++++++++++-------------- SunnyUI/Controls/UITreeView.cs | 2 +- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/SunnyUI/Common/UControl.cs b/SunnyUI/Common/UControl.cs index 2b7fd2bd..de5b807d 100644 --- a/SunnyUI/Common/UControl.cs +++ b/SunnyUI/Common/UControl.cs @@ -18,6 +18,7 @@ * * 2020-01-01: V2.2.0 增加文件说明 * 2023-04-02: V3.3.4 修复关闭弹窗null的Bug + * 2024-11-06: V3.7.2 UITreeView增加了一个刷新节点状态的扩展函数 ******************************************************************************/ using System; @@ -530,32 +531,38 @@ namespace Sunny.UI /// 全选状态(Checked),半选状态(Indeterminate),未选状态(Unchecked) public static CheckState CheckState(this TreeNode node) { - if (GetChildNodeCheckedState(node)) + if (node.Nodes.Count > 0) { - return System.Windows.Forms.CheckState.Indeterminate; + var count = node.Nodes.Cast().Where(n => n.Checked).ToList().Count; + if (count == 0) + { + return System.Windows.Forms.CheckState.Unchecked; + } + else if (count == node.Nodes.Count) + { + return System.Windows.Forms.CheckState.Checked; + } + else + { + 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) + public static void RefreshCheckState(this TreeNode node) { + if (node.TreeView is not UITreeView.TreeViewEx tv) return; + System.Windows.Forms.CheckState state = node.CheckState(); 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); - } - } + if (state == System.Windows.Forms.CheckState.Checked && !node.Checked) node.Checked = true; + if (state == System.Windows.Forms.CheckState.Unchecked && node.Checked) node.Checked = false; } - return false; + + tv.DicNodeStatus[node.GetHashCode()] = state == System.Windows.Forms.CheckState.Indeterminate; + tv.Invalidate(); } public static void Disabled(this Control ctrl) diff --git a/SunnyUI/Controls/UITreeView.cs b/SunnyUI/Controls/UITreeView.cs index 7aedfeef..c9e0b2c7 100644 --- a/SunnyUI/Controls/UITreeView.cs +++ b/SunnyUI/Controls/UITreeView.cs @@ -1416,7 +1416,7 @@ namespace Sunny.UI base.WndProc(ref m); } - private Dictionary DicNodeStatus = new Dictionary(); + internal Dictionary DicNodeStatus = new Dictionary(); protected override void OnAfterCheck(TreeViewEventArgs e) {