2017-04-21 7 views
0

私はコンポーネントを開発しています。このコンポーネントには、TDataSourceプロパティとTSecondaryPathsListプロパティがあります。 TSecondaryPathsListは次のように宣言されています。Delphi - リストでプロパティエディタをドロップダウンしますか?

TSecondaryPathListItem = Class(TCollectionItem) 
    private 
     fDataField: string; 
     fPathPrefixParameter: String; 
     procedure SetDataField(Value: string); 
     procedure SetPathPrefixParameter(Value: String); 
    published 
     property DataField: string read fDataField write SetDataField; 
     property PathPrefixParameter: String read fPathPrefixParameter write SetPathPrefixParameter; 
    End; 

    TSecondaryPathsList = class(TOwnedCollection) 
    private 
    function GetItem(Index: Integer): TSecondaryPathListItem; 
    procedure SetItem(Index: Integer; Value: TSecondaryPathListItem); 
    public 
    function Add: TSecondaryPathListItem; 
    property Items[Index: Integer]: TSecondaryPathListItem read GetItem write SetItem; default; 
    end; 

DataSourceプロパティを持たせたくありません。 TSecondaryPathListItem.DataFieldプロパティを実装して(プロパティエディタの)ドロップダウンリストにし、ComponentのDataSource.DataSetフィールドを表示するにはどうすればよいですか?

答えて

1

DataSourceDataFieldのプロパティは別々のクラスになっていますので、DataFieldプロパティのカスタムプロパティエディタを作成して登録する必要があります。 Delphiの標準TDataFieldPropertyクラスをエディタのベースとして使用できます。 TDataFieldPropertyは通常、プロパティが宣言されているクラスと同じクラスのDataSource: TDataSourceプロパティ(名前はカスタマイズ可能)を検索しますが、それを微調整してメインコンポーネントからTDataSourceオブジェクトを取得することができます。

requires IDEのdesignideおよびdcldbパッケージと、コンポーネントのランタイムパッケージであるデザイン時パッケージを作成します。

unit MyDsgnTimeUnit; 

interface 

uses 
    Classes, DesignIntf, DesignEditors, DBReg; 

type 
    TSecondaryPathListItemDataFieldProperty = class(TDataFieldProperty) 
    public 
    procedure GetValueList(List: TStrings); override; 
    end; 

procedure Register; 

implementation 

uses 
    DB, MyComponentUnit; 

procedure TSecondaryPathListItemDataFieldProperty.GetValueList(List: TStrings); 
var 
    Item: TSecondaryPathListItem; 
    DataSource: TDataSource; 
begin 
    Item := GetComponent(0) as TSecondaryPathListItem; 

    DataSource := GetObjectProp(Item.Collection.Owner, GetDataSourcePropName) as TDataSource; 
    // alternatively: 
    // DataSource := (Item.Collection.Owner as TMyComponent).DataSource; 

    if (DataSource <> nil) and (DataSource.DataSet <> nil) then 
    DataSource.DataSet.GetFieldNames(List); 
end; 

procedure Register; 
begin 
    RegisterPropertyEditor(TypeInfo(string), TSecondaryPathListItem, 'DataField', TSecondaryPathListItemDataFieldProperty); 
end; 

end. 

今、あなたはIDEに設計時パッケージをインストールすることができ、そして、あなたのDataFieldプロパティがで満たされ、ドロップダウンリストが表示されます:TDataFieldPropertyから派生し、このようにその仮想GetValueList()メソッドをオーバーライドしたクラスを実装コンポーネントに割り当てられているTDataSourceのフィールド名。

関連する問題