Delphi-使用列表填充属性编辑器下拉列表?

Delphi-使用列表填充属性编辑器下拉列表?,delphi,properties,components,cascadingdropdown,Delphi,Properties,Components,Cascadingdropdown,我正在开发一个组件。此组件具有TDataSource属性和TSecondaryPathsList属性。TSecondaryPathsList声明如下: TSecondaryPathListItem = Class(TCollectionItem) private fDataField: string; fPathPrefixParameter: String; procedure SetDataField(Value: string);

我正在开发一个组件。此组件具有TDataSource属性和TSecondaryPathsList属性。TSecondaryPathsList声明如下:

  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;
我不希望它有数据源属性。
如何实现TSecondaryPathListItem.DataField属性,使其成为一个下拉列表(在属性编辑器中),显示组件的DataSource.DataSet字段?

您的
数据源
数据字段
属性在不同的类中,因此,您必须为
数据字段
属性编写并注册一个自定义属性编辑器,以将它们链接在一起。您可以使用Delphi的标准
TDataFieldProperty
类作为编辑器的基础
TDataFieldProperty
通常在声明
DataField
属性的同一类中查找
DataSource:TDataSource
属性(名称可自定义),但您可以调整该属性以从主组件检索
TDataSource
对象

创建
需要
IDE的
designide
dcldb
包以及组件的运行时包的设计时包。实现一个从
TDataFieldProperty
派生的类,并重写其虚拟
GetValueList()
方法,如下所示:

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.
unitmydsgntimeunit;
接口
使用
类、DesignIntf、DesignEditor、DBReg;
类型
TSecondaryPathListItemDataFieldProperty=class(TDataFieldProperty)
公众的
程序GetValueList(列表:TStrings);推翻
结束;
程序登记册;
实施
使用
DB,MyComponentUnit;
过程TSecondaryPathListItemDataFieldProperty.GetValueList(列表:TStrings);
变量
项目:TSecondaryPathListItem;
数据源:TDataSource;
开始
Item:=GetComponent(0)作为TSecondaryPathListItem;
DataSource:=GetObjectProp(Item.Collection.Owner,GetDataSourcePropName)作为TDataSource;
//或者:
//数据源:=(Item.Collection.Owner作为TMY组件)。数据源;
如果(DataSource nil)和(DataSource.DataSet nil),则
DataSource.DataSet.GetFieldNames(列表);
结束;
程序登记册;
开始
RegisterPropertyEditor(类型信息(字符串)、TSecondaryPathListItem、“数据字段”、TSecondaryPathListItemDataFieldProperty);
结束;
结束。
现在,您可以将设计时包安装到IDE中,并且您的
DataField
属性应该显示一个下拉列表,其中填充了分配给组件的
TDataSource
中的字段名