Simple hierarchische Datenstruktur - z.B. Ordner und Items. 2 Datatemplates für Ordner und Items, beide an den TVP hängen, ColumnDefinitions erstellen, Liste der Objekte auf der Rootebene an den TVP hängen - fertig.
Minimalcode (vorher VST und TVP auf die Form und dem TVP den VST an seiner TreeView Eigenschaft zuweisen, außerdem eine Column auf dem TVP anlegen)
Delphi-Quellcode:
uses
Spring,
Spring.Collections,
DSharp.Core.DataTemplates;
type
TNamedItem = class
private
fName: string;
public
constructor Create(const name: string);
property Name: string read fName write fName;
end;
TFolder = class(TNamedItem)
private
fItems: IList<TObject>;
public
constructor Create(const name: string);
property Items: IList<TObject> read fItems;
end;
TItemTemplate = class(TDataTemplate<TNamedItem>)
function GetItems(const Item: TNamedItem): IObjectList; override;
function GetValue(const Item: TNamedItem;
const ColumnIndex: Integer): TValue; override;
end;
procedure TTVPDemoForm.FormCreate(Sender: TObject);
var
items: IObjectList;
folder1, folder2: TFolder;
begin
items := TCollections.CreateObjectList<TObject> as IObjectList;
folder1 := TFolder.Create('Folder 1');
folder2 := TFolder.Create('Folder 1.1');
folder2.Items.AddRange([TNamedItem.Create('File 1'), TNamedItem.Create('File 2')]);
folder1.Items.Add(folder2);
items.Add(folder1);
TreeViewPresenter1.View.ItemTemplate := TItemTemplate.Create;
TreeViewPresenter1.View.ItemsSource := items;
end;
{ TNamedItem }
constructor TNamedItem.Create(const name: string);
begin
inherited Create;
fName := name;
end;
{ TFolder }
constructor TFolder.Create(const name: string);
begin
inherited Create(name);
fItems := TCollections.CreateObjectList<TObject>;
end;
{ TItemTemplate }
function TItemTemplate.GetItems(const Item: TNamedItem): IObjectList;
begin
if Item is TFolder then
Result := TFolder(Item).Items as IObjectList
else
Result := nil;
end;
function TItemTemplate.GetValue(const Item: TNamedItem;
const ColumnIndex: Integer): TValue;
begin
Result := Item.Name;
end;
end.