组件属性更改后更新VirtualStringTree标头

时间:2016-03-20 17:52:38

标签: delphi collections components

我想创建一个名为TMyComp的组件。 该组件具有以下相关属性: property VirtualStringTree: TVirtualStringTreeproperty 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;

1 个答案:

答案 0 :(得分:0)

经过一番搜索,这就是答案:

  1. RedrawVirtualStringTreeHeader的触发器已通过FOwner课程内的TMyCompColumns完成。
  2. procedure TMyCompColumns.Update(Item: TCollectionItem); begin inherited; (FOwner as TMyComp).RedrawVirtualStringTreeHeader; end;

    1. 我使用property Caption: String read FCaption write FCaption更新了write SetCaption并添加了程序
    2. procedure TMyCompColumnsItem.SetCaption(const Value: String); begin FCaption:= Value; Changed(False); //---> this will trigger TMyCompColumns.Update end;

      实际上秘密是Changed(False);触发Update

      还要感谢TVirtualStringTree组件的开源。