2016-12-27 57 views
5

Bir Delphi vcl bileşeni yapıyorum, bileşen sınıfında bir TImagelist seçmeme izin veren bir 'images' özelliği var.Delphi bileşen tasarımı - alt özelliğe sahip bileşenden özellik sağlama

Bileşen sınıfında, kendisi de bir imageindex özelliğine sahip bir alt özelliğe sahip 'düğmeler' vardır.

imageindex özelliği için bir görüntüleyici düzenleyicisi yazdım, böylece imgelerdeki düğmelerde bir görüntü seçebiliyorum; Bunu daha önce diğer bileşenlerde yaptım ama şu anda karşı karşıya olduğum problem, 'düğmeler' alt sınıfı etkinliğinde, olaydan temel sınıfın görüntü özelliğini almam gerektiğidir.

Yani, bileşenin temel sınıf bu özelliklere sahiptir:

düğmeleri sınıfı bu özelliği vardır
property Buttons: TFlexButtons read FButtons write FButtons; 
property Images: TCustomImageList read FImages write SetImages; 

:

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

Ben ImageIndex özellik için ayrı ünitede bir özellik editörü kayıt Bir görüntü seçmek için, ancak bu olayda, bileşenlerin temel sınıfından görüntü uzmanını almam gerekiyor, bu özelliği bu alt özellikten nasıl alabilirim?

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; 

Tüm sınıflar:

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; 

cevap

8

tasarım zamanında bir koleksiyon açığa yerine doğrudan TCollection ait TOwnedCollection kullanın. Bu, etkinleştirmek için fazladan kod yazmak zorunda kalmadan DFM akışını kolaylaştırır.

TCollectionItem bu da bir Owner yöntem TOwnedCollection uygular sahip bir Collection özelliğine sahiptir. Bu şekilde, bir düğmeden sahip olduğunuz grup kutusuna kod içinde ulaşabilirsiniz.

bu deneyin: mükemmel ve net örnek kod Remy

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

sayesinde bu sadece iyi iş gibi görünüyor ve şimdi ben sistemini anlıyoruz. –

İlgili konular