问题描述
我有一个从第三方项目文件填充的对象列表。这个文件的设计方式是每个项目都在层次结构的层次上。所以第一个项目是0级,所有的子项都在1级,等等。举个例子:
1.节点1(级别0)
pre>
2.节点1.1(级别1)
3.节点1.2(级别1)
4.节点1.3(级别1)
5.节点1.3.1(级别2)
6.节点1.4(级别1)
7.节点2(级别0)
8.节点2.1(1级)
9.节点2.1.1(级别2)
10.节点3(级别0)
这将产生如下所示的层次结构:
- 节点1
---节点1.1
---节点1.2
---节点1.3
-----节点1.3.1
---节点1.4
- 节点2
---节点2.1
-----节点2.1.1
- 节点3
我的问题是找出如何根据这些级别属性将此结构填充到VCL
TTreeView
每个列出的对象。如果我设计了这个第三方文件结构,我将使用一个父属性而不是一个level属性。
此列表中的对象可以像这样迭代: / p>
var
I:TMyItem;
N:TTreeNode;
begin
for X:= 0 to MyList.Count - 1 do begin
I:= MyList [X];
// TMyItem具有属性Level,它指定层次结构
//以及节点标题的Title属性
//如何基于Level创建节点?
N.Data:= I;
结束
结束
基于此结构,我如何在树视图中填充这个结构?
解决方案尝试如下:
var
项目:TMyItem;
节点:TTreeNode;
NodeLevel:Integer;
X:整数;
begin
Node:= nil;
NodeLevel:= 0;
for X:= 0 to MyList.Count-1 do
begin
Item:= MyList [X];
if(Node = nil)或(Item.Level< = 0)then
begin
Node:= TreeView1.Items.AddObject(nil,Item.Text,Item);
NodeLevel:= 0;
end
else如果Item.Level = NodeLevel then
begin
Node:= TreeView1.Items.AddObject(Node,Item.Text,Item);
end else
begin
而Item.Level< = NodeLevel do
begin
Node:= Node.Parent;
Dec(NodeLevel);
结束
Node:= TreeView1.Items.AddChildObject(Node,Item.Text,Item);
Inc(NodeLevel);
结束
//根据需要设置Node属性...
end;
结束
I have a list of objects populated from a third-party project file. The way this file was designed is so each item is on a "level" of hierarchy. So the very first item is on level 0, all of its child items are on level 1, and so on.
As an example:
1. Node 1 (Level 0) 2. Node 1.1 (Level 1) 3. Node 1.2 (Level 1) 4. Node 1.3 (Level 1) 5. Node 1.3.1 (Level 2) 6. Node 1.4 (Level 1) 7. Node 2 (Level 0) 8. Node 2.1 (Level 1) 9. Node 2.1.1 (Level 2) 10. Node 3 (Level 0)
This would produce a hierarchy like so:
- Node 1 --- Node 1.1 --- Node 1.2 --- Node 1.3 ----- Node 1.3.1 --- Node 1.4 - Node 2 --- Node 2.1 ----- Node 2.1.1 - Node 3
My issue is figuring out how to populate this structure into a VCL
TTreeView
based on these "Level" properties of each listed object. If I had designed this third-party file structure, I would have used a parent property instead of a level property.Objects in this list can be iterated like this:
var I: TMyItem; N: TTreeNode; begin for X := 0 to MyList.Count - 1 do begin I := MyList[X]; //TMyItem has property "Level" which specifies hierarchy // as well as "Title" property for the node's caption //How to create node based on Level? N.Data := I; end; end;
Based on this structure, how do I populate this in a tree view?
解决方案Try something like this:
var Item: TMyItem; Node: TTreeNode; NodeLevel: Integer; X: Integer; begin Node := nil; NodeLevel := 0; for X := 0 to MyList.Count-1 do begin Item := MyList[X]; if (Node = nil) or (Item.Level <= 0) then begin Node := TreeView1.Items.AddObject(nil, Item.Text, Item); NodeLevel := 0; end else if Item.Level = NodeLevel then begin Node := TreeView1.Items.AddObject(Node, Item.Text, Item); end else begin while Item.Level <= NodeLevel do begin Node := Node.Parent; Dec(NodeLevel); end; Node := TreeView1.Items.AddChildObject(Node, Item.Text, Item); Inc(NodeLevel); end; // set Node properties as needed... end; end;
这篇关于如何根据具有“级别”的平面列表填充树视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!