1.给TreeList tlstRegion添加一个自定列(包含)
TreeListColumn IsAll;
RepositoryItemCheckEdit repositoryChk = new RepositoryItemCheckEdit();
chkIsAll.EditValueChanging += chkIsAll_EditValueChanging;
tlstRegion.RepositoryItems.Add(chkIsAll);
this.IsAll.ColumnEdit = chkIsAll; --IsAll是tlstRegion的TreeListColumn
2.给tlstRegion绑定数据
//01.加载区域
tlstRegion.KeyFieldName = "RegionCode"; --关联的子级编号
tlstRegion.ParentFieldName = "ParentCode"; --关联的父级编号
tlstRegion.Nodes.Clear();
CreateNode(new object[] { 0, "区域管理", "0", "", 0, "",false}, 0, true);
LoadTreeData("0", 0, false);
//02.加载树形
private void LoadTreeData(string parentCode, int nodeId, bool isChecked)
{
List<CM_Region> list = _mBusinessCmRegionService.GetRegionListByParentCode(parentCode);
if (list != null && list.Count > 0)
{
foreach (CM_Region item in list)
{
bool hasChildren = _mBusinessCmRegionService.HasChildrenByRegionCode(item.RegionCode); //判断当前节点是否有子节点
CreateNode(new object[] { item.ID, item.RegionName, item.RegionCode, item.ParentCode, item.RegionLevel, EnumHelper.GetEnumItemDesc(typeof(DataDictionary.Status), item.Status), isChecked }, nodeId, hasChildren);
}
if (parentCode == "0")
{
tlstRegion.Nodes[0].Expanded = true;
}
else
{
if (tlstRegion.FocusedNode != null) { tlstRegion.FocusedNode.Expanded = true; }
}
}
}
//03.创建Node
private void CreateNode(object obj, int nodeId, bool hasChildren)
{
tlstRegion.BeginUnboundLoad();
TreeListNode tln = tlstRegion.AppendNode(obj, nodeId);
tln.HasChildren = hasChildren;
tln.ImageIndex = 1;
tlstRegion.EndUnboundLoad();
}
效果图:
3.TreeList 父子节点"包含"状态同步
在chkIsAll_EditValueChanging事件触发
void chkIsAll_EditValueChanging(object sender, ChangingEventArgs e)
{
if (e.NewValue == null)
{
e.Cancel = true;
return;
}
bool check = DataTypeConvert.ToBoolean(e.NewValue);
TreeListNode node = tlstRegion.FocusedNode;
if (node!=null)
{
node["IsAll"] = check;
}
//01.父节点往下面展开操作
SetCheckedChildNodes(node, check);
//02.字节点往上找
SetCheckedParentNodes(node, check);
}
/// <summary>
/// 全选子节点
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedChildNodes(TreeListNode node, bool check)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i]["IsAll"] = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}
/// <summary>
/// 父节点
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedParentNodes(TreeListNode node, bool check)
{
if (node.ParentNode != null)
{
int result = 0;//√
int reuslt1 = 0;//不打√
int result2 = 0;//方块
string parentCheck = node.ParentNode["IsAll"].ToString();//父节点
for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
{
string nodeCheck = node.ParentNode.Nodes[i]["IsAll"].ToString();
if (nodeCheck.ToLower() == "true")
{
result++;
}
else if (nodeCheck.ToLower() == "false")
{
reuslt1++;
}
else
{
result2++;
}
}
if (node.ParentNode.Nodes.Count == result)//01.打√数量和父级数量一致则:父级√
{
node.ParentNode["IsAll"] = true;
}
else if (result > 0)//02.只有有1个打钩:父级为方块
{
node.ParentNode["IsAll"] = CheckState.Indeterminate;
}
else if (node.ParentNode.Nodes.Count == reuslt1)//03.不打√数量和父级数量一致:父级不√
{
node.ParentNode["IsAll"] = false;
}
else if (check && result2 > 0)//04.只要有一个是方块则:父级为方块
{
node.ParentNode["IsAll"] = CheckState.Indeterminate;
}
SetCheckedParentNodes(node.ParentNode, check);
}
效果图: