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);
}
效果圖: