Код:
unit Unit2;
interface
uses
Classes;
type
TMyCollectionItem = class(TCollectionItem)
private
FId: Integer;
FName: String;
public
constructor Create(Collection: TCollection); override;
published
property Id: Integer read FId write FId;
property Name: String read FName write FName;
end;
TMyComponent = class(TComponent)
private
FMyCollection: TOwnedCollection;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property MyCollection: TOwnedCollection read FMyCollection write FMyCollection;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('My', [TMyComponent]);
end;
{ TMyCollectionItem }
constructor TMyCollectionItem.Create(Collection: TCollection);
begin
inherited Create(Collection);
FId:=0;
FName:='';
end;
{ TMyComponent }
constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyCollection:=TOwnedCollection.Create(Self, TMyCollectionItem);
end;
destructor TMyComponent.Destroy;
begin
FMyCollection.Free;
inherited Destroy;
end;
end.
но лучше так не делать - не удобно работать с такой коллекцией.
правильней создать свой класс коллекцию, унаследовавшись от TOwnedCollection или TCollection (реализовав GetOwner). задать свой тип у Items, Add, Insert и т.д.