2016-12-27 24 views
5

私はDelphiのvclコンポーネントを作成しています。コンポーネントクラスには、TImagelistを選択できる 'images'プロパティがあります。Delphiコンポーネントの設計 - サブプロパティからのコンポーネントからプロパティを取得

コンポーネントクラスには、それ自身がimageindexプロパティを持つサブプロパティ 'buttons'もあります。

imageindexプロパティのコンポーネントエディタを作成して、イメージリストからボタン上のイメージを選択できるようにしました。私は前に他のコンポーネントでこれを行っていますが、私が直面している問題は、 'buttons'サブクラスイベントのイベントから基本クラスのイメージプロパティを取得する必要があることです。

ので、コンポーネントの基本クラスは、これらのプロパティがあります。

property Buttons: TFlexButtons read FButtons write FButtons; 
property Images: TCustomImageList read FImages write SetImages; 

ボタンクラスは、このプロパティを持っています

property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1; 

私はImageIndexプロパティのために別々の単位でプロパティエディタを登録します画像を選択するには、このイベントでコンポーネントのベースクラスからイメージリストを取得する必要がありますが、このサブプロパティからこのプロパティを取得するにはどうすればよいですか?

function TImageIndexProperty.GetImageListAt(Index: Integer): TCustomImageList; 
var APersistent: TPersistent; 
begin 
    APersistent := GetComponent(Index); 
    if APersistent is TFlexButton then 
    Result := ??????????.Images //how do i refer to the images property of the component here? 
    else 
    Result := nil; 
end; 

すべてのクラスは:

TFlexButton = class(TCollectionItem) 
private 
    FWidth: Word; 
    FCaption: string; 
    FHeight: Word; 
    FImageIndex: TImageIndex; 
    procedure SetCaption(const Value: string); 
    procedure SetHeight(const Value: Word); 
    procedure SetWidth(const Value: Word); 
    procedure SetImageIndex(const Value: TImageIndex); 
public 
    constructor Create(AOwner: TComponent); 
    destructor Destroy; override; 
published 
    property Caption: string read FCaption write SetCaption; 
    property Height: Word read FHeight write SetHeight; 
    property Width: Word read FWidth write SetWidth; 
    property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1; 
end; 

TFlexButtons = class(TCollection) 
private 
    function GetItem(Index: Integer): TFlexButton; 
public 
    function Add: TFlexButton; 
    property Item[index: Integer]: TFlexButton read GetItem; 
end; 

TFlexButtonGroupBox = class(TcxGroupBox) 
private 
    FDataLink: TFieldDataLink; 
    FAbout: string; 
    FAlignment: TAlignment; 
    FEnabled: Boolean; 
    FButtons: TFlexButtons; 
    FImages: TCustomImageList; 
    FSql: TStrings; 
    FAutosize: Boolean; 
    procedure SetAlignment(const Value: TAlignment); 
    function GetDataField: string; 
    function GetDataSource: TdataSource; 
    procedure SetDataField(const Value: string); 
    procedure SetDataSource(const Value: TdataSource); 
    procedure DataChange(Sender: TObject); 
    procedure SetEnabled(const Value: Boolean); 
    procedure SetImages(const Value: TCustomImageList); 
    procedure SetSql(const Value: TStrings); 
    procedure SetAutosize(const Value: Boolean); 
protected 
public 
    procedure Loaded; override; 
    constructor Create(AOwner: TComponent); override; 
    destructor Destroy; override; 
published 
    property DataField: string read GetDataField write SetDataField; 
    property DataSource: TdataSource read GetDataSource write SetDataSource; 
    property Enabled: Boolean read FEnabled write SetEnabled; 
    property Autosize: Boolean read FAutosize write SetAutosize; 
    property About: string read FAbout write FAbout; 
    property Buttons: TFlexButtons read FButtons write FButtons; 
    property Images: TCustomImageList read FImages write SetImages; 
    property Alignment: TAlignment read FAlignment write SetAlignment; 
    property Sql: TStrings read FSql write SetSql; 
end; 

答えて

8

設計時にコレクションを露出させ、代わりに直接TCollectionTOwnedCollection使用します。これにより、DFMストリーミングが可能になるための余分なコードを書く必要がなくなります。

TCollectionItemは、Collectionの特性を有し、次に、TOwnedCollectionの実装方法を有する。こうすることで、ボタンからコード内の所有グループボックスに移動できます。

これを試してみてください:優秀で、明確なサンプルコードレミーのため

TFlexButton = class(TCollectionItem) 
private 
    ... 
public 
    constructor Create(ACollection: TCollection); override; 
end; 

TFlexButtonGroupBox = class; 

TFlexButtons = class(TOwnedCollection) 
private 
    ... 
public 
    constructor Create(AOwner: TFlexButtonGroupBox); reintroduce; 
    ... 
end; 

TFlexButtonGroupBox = class(TcxGroupBox) 
private 
    ... 
    procedure SetButtons(AValue: TFlexButtons; 
public 
    constructor Create(AOwner: TComponent); override; 
    ... 
published 
    ... 
    property Buttons: TFlexButtons read FButtons write SetButtons; 
    ... 
end; 

constructor TFlexButton.Create(ACollection: TCollection); 
begin 
    inherited; 
    ... 
end; 

constructor TFlexButtons.Create(AOwner: TFlexButtonGroupBox); 
begin 
    inherited Create(AOwner, TFlexButton); 
    ... 
end; 

constructor TFlexButtonGroupBox.Create(AOwner: TComponent); 
begin 
    inherited; 
    FButtons := TFlexButtons.Create(Self); 
    ... 
end; 

procedure TFlexButtonGroupBox.SetButtons(AValue: TFlexButtons; 
begin 
    FButtons.Assign(AValue); 
end; 

function TImageIndexProperty.GetImageListAt(Index: Integer): TCustomImageList; 
begin 
    Result := ((GetComponent(Index) as TFlexButton).Collection.Owner as TFlexButtonGroupBox).Images; 
end; 
+1

おかげで、これはうまく動作するようだと、今私は、システムを理解しています。 –

関連する問題