*UITreeView: 判断节点Checked是否改变,只有改变时才赋值

This commit is contained in:
Sunny 2021-10-12 11:15:37 +08:00
parent 904aa0bb9e
commit abdfd501cb
3 changed files with 35 additions and 30 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,26 +1,26 @@
/****************************************************************************** /******************************************************************************
* SunnyUI * SunnyUI
* CopyRight (C) 2012-2021 ShenYongHua(). * CopyRight (C) 2012-2021 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com * QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
* *
* Blog: https://www.cnblogs.com/yhuse * Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI * Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI * GitHub: https://github.com/yhuse/SunnyUI
* *
* SunnyUI.dll can be used for free under the GPL-3.0 license. * SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note. * If you use this code, please keep this note.
* 使 * 使
****************************************************************************** ******************************************************************************
* : UITreeView.cs * : UITreeView.cs
* : * :
* : V3.0 * : V3.0
* : 2020-05-05 * : 2020-05-05
* *
* 2020-05-05: V2.2.5 * 2020-05-05: V2.2.5
* 2020-07-07: V2.2.6 CheckBoxes等 * 2020-07-07: V2.2.6 CheckBoxes等
* 2020-08-12: V2.2.7 * 2020-08-12: V2.2.7
* 2021-07-19: V3.0.5 CheckBoxes时图片位置 * 2021-07-19: V3.0.5 CheckBoxes时图片位置
* 2021-08-26: V3.0.6 CheckBoxes增加三态 * 2021-08-26: V3.0.6 CheckBoxes增加三态
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -740,9 +740,9 @@ namespace Sunny.UI
} }
else else
{ {
var drawOffsetX = e.Node.Bounds.X-57-e.Node.Level * Indent;
var drawLeft = (e.Node.Level + 1) * Indent + 3+ drawOffsetX; var drawLeft = (e.Node.Level + 1) * Indent + 3;
var checkBoxLeft = (e.Node.Level + 1) * Indent + 1+ drawOffsetX; var checkBoxLeft = (e.Node.Level + 1) * Indent + 1;
var imageLeft = drawLeft; var imageLeft = drawLeft;
var haveImage = false; var haveImage = false;
var sf = e.Graphics.MeasureString(e.Node.Text, Font); var sf = e.Graphics.MeasureString(e.Node.Text, Font);
@ -801,7 +801,9 @@ namespace Sunny.UI
} }
if (CheckBoxes) if (CheckBoxes)
{ {
if (!e.Node.Checked) if (!e.Node.Checked)
{ {
e.Graphics.DrawRectangle(checkboxColor, e.Graphics.DrawRectangle(checkboxColor,
@ -838,7 +840,7 @@ namespace Sunny.UI
} }
var lineY = e.Bounds.Y + e.Node.Bounds.Height / 2 - 1; var lineY = e.Bounds.Y + e.Node.Bounds.Height / 2 - 1;
var lineX = 3 + e.Node.Level * Indent + 9+drawOffsetX ; var lineX = 3 + e.Node.Level * Indent + 9;
if (ShowLinesEx) if (ShowLinesEx)
{ {
@ -892,7 +894,7 @@ namespace Sunny.UI
} }
} }
lineX = 3 + e.Node.Level * Indent + 9 + drawOffsetX; lineX = 3 + e.Node.Level * Indent + 9;
//绘制左侧+号 //绘制左侧+号
if (ShowPlusMinus && e.Node.Nodes.Count > 0) if (ShowPlusMinus && e.Node.Nodes.Count > 0)
{ {
@ -931,7 +933,8 @@ namespace Sunny.UI
private Dictionary<int, bool> DicNodeStatus = new Dictionary<int, bool>(); private Dictionary<int, bool> DicNodeStatus = new Dictionary<int, bool>();
protected override void OnAfterCheck(TreeViewEventArgs e) protected override void OnAfterCheck(TreeViewEventArgs e)
{ {
base.OnAfterCheck(e); base.OnAfterCheck(e);
if (e.Action == TreeViewAction.ByMouse) //鼠标点击 if (e.Action == TreeViewAction.ByMouse) //鼠标点击
{ {
@ -953,12 +956,14 @@ namespace Sunny.UI
TreeNode parentNode = currNode.Parent; //获得当前节点的父节点 TreeNode parentNode = currNode.Parent; //获得当前节点的父节点
var count = parentNode.Nodes.Cast<TreeNode>().Where(n => n.Checked).ToList().Count; var count = parentNode.Nodes.Cast<TreeNode>().Where(n => n.Checked).ToList().Count;
//判断节点Checked是否改变只有改变时才赋值否则不变更以防止频繁触发OnAfterCheck事件 //判断节点Checked是否改变只有改变时才赋值否则不变更以防止频繁触发OnAfterCheck事件
bool bChecked = count == parentNode.Nodes.Count; bool bChecked = count == parentNode.Nodes.Count;
if(parentNode.Checked != bChecked) if (parentNode.Checked != bChecked)
{ {
parentNode.Checked = bChecked; parentNode.Checked = bChecked;
} }
var half = parentNode.Nodes.Cast<TreeNode>().Where(n => (DicNodeStatus.ContainsKey(n.GetHashCode()) ? DicNodeStatus[n.GetHashCode()] : false)).ToList().Count; var half = parentNode.Nodes.Cast<TreeNode>().Where(n => (DicNodeStatus.ContainsKey(n.GetHashCode()) ? DicNodeStatus[n.GetHashCode()] : false)).ToList().Count;
if ((count > 0 && count < parentNode.Nodes.Count) || half > 0) if ((count > 0 && count < parentNode.Nodes.Count) || half > 0)