In in a 'small "g"' generic fashion, the Data
property of a FMX control should get or set the control's core value. In a TImage
case this will be the bitmap displayed, for a TEdit
the text and so on. As such, its purpose is completely different to the Data
property of a VCL tree view item, which is to hang an arbitrary piece of data off of the object.
As pani rightly answers, if you want to hang an arbitrary object reference to a FMX tree view item, then you can use TagObject
. That said, and notwithstanding irritations concerning FMX's bodging of proper OOP behaviour (see here), if you are creating tree view items dynamically then a better way might be to derive a custom TTreeViewItem
descendant:
uses System.Generics.Collections;
type
TRoomTreeViewItem = class(TTreeViewItem)
RoomList: TList<TRoom>; //better use a generic than non-generic list as mentioned above
end;
Or, if the lifetime of a room list is the same as the lifetime of the tree view item it is associated with, you could actually encapsulate the list in the item:
type
TRoomTreeViewItem = class(TTreeViewItem)
strict private
FRoomList: TObjectList<TRoom>;
function GetRoom(Index: Integer): TRoom;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetEnumerator: TEnumerator<TRoom>;
function AddRoom: TRoom;
property Rooms[Index: Integer] read GetRoom;
end;
constructor TRoomTreeViewItem.Create(AOwner: TComponent);
begin
inherited;
FRoomList := TObjectList<TRoom>.Create;
end;
destructor TRoomTreeViewItem.Destroy;
begin
FRoomList.Free;
inherited;
end;
function TRoomTreeViewItem.GetEnumerator: TEnumerator<TRoom>;
begin
Result := FRoomList.GetEnumerator;
end;
function TRoomTreeViewItem.GetRoom(Index: Integer): TRoom;
begin
Result := FRoomList[Index];
end;
function TRoomTreeViewItem.AddRoom: TRoom;
begin
Result := TRoom.Create;
FRoomList.Add(Result);
end;
Some people may consider the second variant a terrible conflation of non-UI with UI code however - personally I don't oppose it (indeed, that's why I've suggested it), though YMMV.