unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, System.Generics.Collections,
Dialogs, ContNrs, VirtualTrees;
type
TNamedItem = class
private
fName: string;
fItems: TObjectList<TNamedItem>;
public
constructor Create(const name: string);
property Items: TObjectList<TNamedItem> read fItems;
property Name: string read fName write fName;
end;
TForm1 = class(TForm)
VirtualStringTree1: TVirtualStringTree;
procedure FormCreate(Sender: TObject);
procedure VirtualStringTree1InitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
procedure VirtualStringTree1InitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode; var ChildCount: Cardinal);
procedure VirtualStringTree1GetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
private
{ Private-Deklarationen }
FVstRootItemList: TObjectList<TNamedItem>;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
folder1: TNamedItem;
folder1_1, folder1_2, folder1_3: TNamedItem;
folder1_1_1: TNamedItem;
begin
FVstRootItemList := TObjectList<TNamedItem>.Create;
folder1 := TNamedItem.Create('Folder 1');
folder1_1 := TNamedItem.Create('Folder 1.1');
folder1_1_1 := TNamedItem.Create('Folder 1.1.1');
folder1_1.Items.Add(folder1_1_1);
folder1.Items.Add(folder1_1);
FVstRootItemList.Add(folder1);
folder1 := TNamedItem.Create('node');
folder1_1 := TNamedItem.Create('subnode');
folder1_2 := TNamedItem.Create('subnode');
folder1_3 := TNamedItem.Create('subnode');
folder1.Items.Add(folder1_1);
folder1.Items.Add(folder1_2);
folder1.Items.Add(folder1_3);
FVstRootItemList.Add(folder1);
folder1 := TNamedItem.Create('node');
folder1_1 := TNamedItem.Create('subnode');
folder1_2 := TNamedItem.Create('subnode');
folder1_3 := TNamedItem.Create('subnode');
folder1.Items.Add(folder1_1);
folder1.Items.Add(folder1_2);
folder1.Items.Add(folder1_3);
FVstRootItemList.Add(folder1);
VirtualStringTree1.NodeDataSize := SizeOf(TNamedItem);
VirtualStringTree1.RootNodeCount := FVstRootItemList.Count;
end;
procedure TForm1.VirtualStringTree1GetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
var
NamedItem: TNamedItem;
begin
NamedItem := Sender.GetNodeData<TNamedItem>(Node);
if not assigned(NamedItem) then
exit;
case Column of
0:
CellText := NamedItem.name;
end;
end;
procedure TForm1.VirtualStringTree1InitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode; var ChildCount: Cardinal);
var
NamedItem: TNamedItem;
begin
NamedItem := Sender.GetNodeData<TNamedItem>(Node);
ChildCount := NamedItem.Items.Count;
end;
procedure TForm1.VirtualStringTree1InitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
var
NamedItem: TNamedItem;
begin
if VirtualStringTree1.GetNodeLevel(Node) = 0 then begin
NamedItem := FVstRootItemList[Node.Index];
Node.SetData<TNamedItem>(NamedItem)
end
else
begin
NamedItem := Sender.GetNodeData<TNamedItem>(ParentNode).Items[Node.Index];
Node.SetData<TNamedItem>(NamedItem);
end;
if NamedItem.Items.Count > 0 then
InitialStates := InitialStates + [ivsHasChildren, ivsExpanded];
end;
{ TNamedItem }
constructor TNamedItem.Create(const name: string);
begin
fName := name;
fItems := TObjectList<TNamedItem>.Create;
end;
end.