注:
点击时请正常速度点击,不然会出现“奇怪”现象!!! /// <summary>
/// 节点点击 子级->同级->父级
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvRoles_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action != TreeViewAction.ByMouse) return;
ChildrenChecked(e.Node, e.Node.Checked);
ParentChecked(e.Node);
} /// <summary>
/// 把每一个父级当作子级
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private void ParentChecked(TreeNode node)
{
if (node.Parent == null) return;
///单根
//if (node.PrevNode == null && node.NextNode == null)
//{
// node.Parent.Checked = node.Checked;
// return;
//}
///多根
//当前节点两种状态
if (!node.Checked)//节点没被选中,父节点不被选中
{
node.Parent.Checked = node.Checked;
}
else//节点被选中,同级节点决定父节点状态
{
node.Parent.Checked = PeerChecked(node);
}
ParentChecked(node.Parent);//继续上级忽略同级
}
private bool PeerChecked(TreeNode node)
{
if (node == null) return true;
return PeerPrevNodeChecked(node.PrevNode) && PeerNextNodeChecked(node.NextNode);
}
private bool PeerNextNodeChecked(TreeNode node)
{
if (node == null) return true;
return PeerNextNodeChecked(node.NextNode) & node.Checked;
}
private bool PeerPrevNodeChecked(TreeNode node)
{
if (node == null) return true;
if (node.Checked)
return PeerPrevNodeChecked(node.PrevNode);
return false;
}
/// <summary>
/// 把每一个子级当作父级
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void ChildrenChecked(TreeNode node, bool check)
{
foreach (TreeNode item in node.Nodes)
{
ChildrenChecked(item, check);
item.Checked = check;
}
}