1、节点遍历
Items是按树型结构存放的,单纯遍历Items,只能得到第一层的节点,网上很多例子,都是用递归处理,其实是不必的,TcxTreeList包含了一个AbsoluteItems,保存了顺序结构的节点,可以如下遍历:
for i := 0 to ATreeList.AbsoluteCount - 1 do
showmessage(ATreeList.AbsoluteItems[i].Texts[0]);
2、为节点增加CheckBox
ATreeList.OptionsView.CheckGroups := True; //使CheckBox有效
设置了该属性后,节点不会发生任何变化,可以使用遍历设置属性,例如:
for i := 0 to ATreeList.AbsoluteCount - 1 do
ATreeList.AbsoluteItems[i].CheckGroupType := ncgCheckGroup; //多选或单选
这样处理的话,每次加载节点之后都需要执行这项操作,比较麻烦,可以直接修改源代码cxTL.pas,实现自动切换,修改如下:
为TcxCustomTreeList增加属性:
private
FCheckGroupType:TcxTreeListNodeCheckGroupType;
public
property CheckGroupType: TcxTreeListNodeCheckGroupType read FCheckGroupType write SetCheckGroupType; default ncgNone;
//修改创建方法
constructor TcxCustomTreeList.Create(AOwner: TComponent);
begin
...原代码...
FCheckGroupType := ncgNone;
end;
//设置CheckGroupType时,修改节点状态
procedure TcxCustomTreeList.SetCheckGroupType(
const Value: TcxTreeListNodeCheckGroupType);
var
i: Integer;
begin
if FCheckGroupType <> Value then begin
FCheckGroupType := Value;
BeginUpdate;
try
FRoot.CheckGroupType := Value;
for i := 0 to AbsoluteCount - 1 do
AbsoluteItems[i].CheckGroupType := Value;
finally
EndUpdate;
end;
end;
end;
修改节点创建事件:
constructor TcxTreeListNode.Create(AOwner: TcxCustomTreeList);
begin
...原代码...
//创建时设默认属性,忽略OptionsView.CheckGroups的值,无影响
if (AOwner <> nil) then
CheckGroupType := AOwner.CheckGroupType;
end;
3、遍历选中的节点
for i := 0 to AbsoluteCount - 1 do
if AbsoluteItems[i].Checked then showmessage(AbsoluteItems[i].Texts[0]);
4、几个属性
TopNode:顶节点,实际上是第一个有效的节点
Root:第一层节点数组
Items:节点的子节点数组(如果是TreeList.Items相当于Root)
Texts,Values:值数组,对应Bands的Index