unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs, DSharp.Windows.ColumnDefinitions,
DSharp.Windows.CustomPresenter, DSharp.Windows.TreeViewPresenter, VirtualTrees
, Spring
, Spring.Collections
, DSharp.Core.DataTemplates
,
Vcl.StdCtrls
, DSharp.Bindings.Notifications
, DSharp.Core.PropertyChangedBase
;
type
TForm1 =
class(TForm)
VirtualStringTree1: TVirtualStringTree;
TreeViewPresenter1: TTreeViewPresenter;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
Items: IList<TObject>;
public
{ Public-Deklarationen }
end;
TNamedItem =
class(TPropertyChangedBase)
private
fName:
string;
FValue: Integer;
FItems: IList<TObject>;
procedure SetValue(aValue: Integer);
public
constructor Create(
const name:
string);
property Name:
string read fName
write fName;
property Value: Integer
read FValue
write SetValue;
property Items: IList<TObject>
read FItems
write FItems;
end;
TItemTemplate =
class(TDataTemplate<TNamedItem>)
function GetItems(
const Item: TNamedItem): IObjectList;
override;
function GetText(
const Item: TNamedItem;
const ColumnIndex: Integer):
string;
override;
end;
var
Form1: TForm1;
implementation
uses DSharp.Collections.ObservableCollection;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
RootItem: TNamedItem;
SubItem: TNamedItem;
begin
RootItem := TNamedItem(items[0]);
SubItem := RootItem.Items[0]
as TNamedItem;
SubItem.Value := RootItem.Value + 1;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
folder1, folder2: TNamedItem;
begin
items := TObservableCollection<TObject>.Create();
folder1 := TNamedItem.Create('
Folder 1');
folder1.Value := 1;
folder2 := TNamedItem.Create('
Folder 1.1');
folder2.Items.AddRange([TNamedItem.Create('
File 1'), TNamedItem.Create('
File 2')]);
folder1.Items.Add(folder2);
items.Add(folder1);
folder1 := TNamedItem.Create('
Folder 2');
items.Add(folder1);
TreeViewPresenter1.View.ItemTemplate := TItemTemplate.Create;
TreeViewPresenter1.View.ItemsSource := items
as IObjectList;
end;
{ TNamedItem }
constructor TNamedItem.Create(
const name:
string);
begin
inherited Create;
fName :=
name;
Fitems := TObservableCollection<TObject>.Create();
// auch in den Unterknoten muß die Liste mit TObservableCollection<TObject>.Create() erzeugt werden!
end;
procedure TNamedItem.SetValue(aValue: Integer);
begin
if FValue = aValue
then exit;
FValue := aValue;
NotifyOfPropertyChange('
Value');
end;
{ TItemTemplate }
function TItemTemplate.GetItems(
const Item: TNamedItem): IObjectList;
begin
if Item.Items.Count>0
then
Result := TNamedItem(Item).Items
as IObjectList
else
Result :=
nil;
end;
function TItemTemplate.GetText(
const Item: TNamedItem;
const ColumnIndex: Integer):
string;
begin
case ColumnIndex
of
-1, 0: Result := Item.
Name;
1: Result := IntToStr(Item.Value);
end;
end;
end.