我从来没有遇到过需要它的情况,这是我第一次尝试将TCollection作为另一个TCollection的TCollectionItem。它都编译得很好,但是当单击TCollectionItem的TCollection属性后面的三个点时,没有反应,即没有出现带有子TCollection列表的对话框。
我的印象是,由于不需要任何花哨的属性编辑器(子TCollection只包含具有string和single属性的项),IDE将自动处理它。
很明显,事实并非如此,或者我在监督显而易见的事情,这是一种长期困扰。
实现(运行时)单元如下:
type
TBitmapItemTag = class(TCollectionItem)
private
FTagName: string;
FTagFloat: Single;
published
property TagName: string read FTagName write FTagName;
property TagFloat: Single read FTagFloat write FTagFloat;
end;
TBitmapItemTags = class(TOwnedCollection)
end;
TBitmapItem = class(TCollectionItem)
private
FBitmap: TBitmap;
FBitmapItemTags: TBitmapItemTags;
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
published
property Bitmap: TBitmap read FBitmap write FBitmap;
property Tags: TBitmapItemTags read FBitmapItemTags write FBitmapItemTags;
end;
TBitmaps = class(TCollection)
end;
TBitmapCollection = class(TComponent)
private
FBitmaps: TBitmaps;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Bitmaps: TBitmaps read FBitmaps write FBitmaps;
end;
implementation
{ TBitmapItem }
constructor TBitmapItem.Create(Collection: TCollection);
begin
inherited Create(Collection);
FBitmap := TBitmap.Create(0, 0);
FBitmapItemTags := TBitmapItemTags.Create(Self,TBitmapItemTag);
end;
destructor TBitmapItem.Destroy;
begin
FBitmap.Free;
FBitmapItemTags.Free;
inherited;
end;
{ TBitmapCollection }
constructor TBitmapCollection.Create(AOwner: TComponent);
begin
inherited;
FBitmaps := TBitmaps.Create(TBitmapItem);
end;
destructor TBitmapCollection.Destroy;
begin
FBitmaps.Free;
inherited;
end;Register过程在设计时单元中实现,只调用RegisterComponents过程.并进行了一些懒惰的RegisterPropertyEditor尝试,但都没有成功。
如果有人能为我指出最短的路径,以便IDE识别TBitmapItemTag TCollectionItem,我将不胜感激。
发布于 2018-04-24 18:11:01
您需要将TBitmaps更改为从TOwnedCollection派生而不是从TCollection派生。
我还建议为TBitmapItemTags和TBitmaps定义显式构造函数。
您还需要向基于对象的属性添加一些setter方法,否则在运行时可能会发生内存泄漏。您的设置程序应该在对象上调用Assign(),以便将属性值从一个对象复制到另一个对象。TCollection已经为您实现了Assign(),但是您必须在集合项中实现Assign()。
试试这个:
type
TBitmapItemTag = class(TCollectionItem)
private
FTagName: string;
FTagFloat: Single;
public
procedure Assign(ASource: TPersistent); override;
published
property TagName: string read FTagName write FTagName;
property TagFloat: Single read FTagFloat write FTagFloat;
end;
TBitmapItem = class;
TBitmapItemTags = class(TOwnedCollection)
public
constructor Create(AOwner: TBitmapItem); reintroduce;
end;
TBitmapItem = class(TCollectionItem)
private
FBitmap: TBitmap;
FTags: TBitmapItemTags;
procedure SetBitmap(AValue: TBitmap);
procedure SetTags(AValue: TBitmapItemTags);
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(ASource: TPersistent); override;
published
property Bitmap: TBitmap read FBitmap write SetBitmap;
property Tags: TBitmapItemTags read FTags write SetTags;
end;
TBitmapCollection = class;
TBitmaps = class(TOwnedCollection)
public
constructor Create(AOwner: TBitmapCollection); reintroduce;
end;
TBitmapCollection = class(TComponent)
private
FBitmaps: TBitmaps;
procedure SetBitmaps(AValue: TBitmaps);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Bitmaps: TBitmaps read FBitmaps write SetBitmaps;
end;
{ TBitmapTagItem }
procedure TBitmapItemTag.Assign(ASource: TPersistent);
begin
if ASource is TBitmapItemTag then
begin
FTagName := TBitmapItemTag(ASource).TagName;
FTagFloat := TBitmapItemTag(ASource).TagFloat;
end
else
inherited;
end;
{ TBitmapItemTags }
constructor TBitmapItemTags.Create(AOwner: TBitmapItem);
begin
inherited Create(AOwner, TBitmapItemTag);
end;
{ TBitmapItem }
constructor TBitmapItem.Create(Collection: TCollection);
begin
inherited Create(Collection);
FBitmap := TBitmap.Create(0, 0);
FTags := TBitmapItemTags.Create(Self);
end;
destructor TBitmapItem.Destroy;
begin
FBitmap.Free;
FTags.Free;
inherited;
end;
procedure TBitmapItem.Assign(ASource: TPersistent);
begin
if ASource is TBitmapItem then
begin
FBitmap.Assign(TBitmapItem(ASource).Bitmap);
FTags.Assign(TBitmapItem(ASource).Tags);
end
else
inherited;
end;
procedure TBitmapItem.SetBitmap(AValue: TBitmap);
begin
FBitmap.Assign(AValue);
end;
procedure TBitmapItem.SetTags(AValue: TBitmapItemTags);
begin
FTags.Assign(AValue);
end;
{ TBitmaps }
constructor TBitmaps.Create(AOwner: TBitmapCollection);
begin
inherited Create(AOwner, TBitmapItem);
end;
{ TBitmapCollection }
constructor TBitmapCollection.Create(AOwner: TComponent);
begin
inherited;
FBitmaps := TBitmaps.Create(Self);
end;
destructor TBitmapCollection.Destroy;
begin
FBitmaps.Free;
inherited;
end;
procedure TBitmapCollection.SetBitmaps(AValue: TBitmaps);
begin
FBitmaps.Assign(AValue);
end;https://stackoverflow.com/questions/50003211
复制相似问题