我想创建一个名为TMyComp
的组件。
该组件具有以下相关属性:
property VirtualStringTree: TVirtualStringTree
和
property Columns: TMyCompColumns
作为项目集合。
我的组件中的列与关联的VirtualStringTree中的标题列相同。
我想要做的是,在更新标题时,在设计时重绘VirtualStringTree中的标题文本。
我的问题是,我不知道如何触发procedure RedrawVirtualStringTreeHeader
,因为课程TMyCompColumns
甚至TMyCompColumnsItem
都不知道。{1}}。
TMyCompColumnsItem = class(TCollectionItem)
private
FCaption: String;
function GetPosition: Integer;
protected
function GetDisplayName: String; override;
procedure SetIndex(Value: Integer);
public
constructor Create(Collection: TCollection); override;
procedure Assign(Source: TPersistent); override;
published
property Caption: String read FCaption write FCaption;
end;
TMyCompColumns= class(TCollection)
private
FOwner: TComponent;
protected
function GetOwner: TPersistent; override;
function GetItem(Index: Integer): TMyCompColumnsItem;
procedure SetItem(Index: Integer; Value: TMyCompColumnsItem);
procedure Update(Item: TMyCompColumnsItem);
public
constructor Create(AOwner: TComponent);
function Add: TMyCompColumnsItem;
property Items[Index: Integer]: TMyCompColumnsItem read GetItem write SetItem;
end;
TMyComp = class(TComponent)
private
FColumns: TMyCompColumns;
FVirtualStringTree: TVirtualStringTree;
procedure SetMyCompColumns(const Value: TMyCompColumns);
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Columns: TMyCompColumns read FColumns write SetMyCompColumns;
property VirtualStringTree: TVirtualStringTree read FVirtualStringTree write FVirtualStringTree;
end;
...
function TMyCompColumnsItem.GetDisplayName: String;
begin
Result:= FCaption;
RedrawVirtualStringTreeHeader; //<--- procedure not recognized!!!
end;
...
procedure TMyCompColumns.Update(Item: TMyCompColumnsItem);
begin
inherited;
//RedrawVirtualStringTreeHeader; ???or here
end;
procedure TMyComp.RedrawVirtualStringTreeHeader;
var
i: Integer;
begin
if Assigned(FVirtualStringTree) then
begin
FVirtualStringTree.Header.Options:= FVirtualStringTree.Header.Options + [hoVisible];
FVirtualStringTree.Header.Columns.Clear;
if FColumns.Count > 0 then
for i := 0 to FColumns.Count-1 do
with FVirtualStringTree.Header.Columns.Add do
begin
Text:= FColumns.Items[i].Caption;
//...
end;
end;
end;
答案 0 :(得分:0)
经过一番搜索,这就是答案:
RedrawVirtualStringTreeHeader
的触发器已通过FOwner
课程内的TMyCompColumns
完成。 procedure TMyCompColumns.Update(Item: TCollectionItem);
begin
inherited;
(FOwner as TMyComp).RedrawVirtualStringTreeHeader;
end;
property Caption: String read FCaption write FCaption
更新了write SetCaption
并添加了程序 procedure TMyCompColumnsItem.SetCaption(const Value: String);
begin
FCaption:= Value;
Changed(False); //---> this will trigger TMyCompColumns.Update
end;
实际上秘密是Changed(False);
触发Update
还要感谢TVirtualStringTree
组件的开源。