首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Delphi -用list填充属性编辑器下拉列表?

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

Stack Overflow用户
提问于 2017-04-21 14:37:17
回答 1查看 958关注 0票数 0

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

代码语言:javascript
复制
  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属性,使其成为dropDown列表(在属性编辑器中),显示组件的DataSource.DataSet字段?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-21 15:31:33

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

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

代码语言:javascript
复制
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属性应该显示一个下拉列表,其中包含来自分配给组件的任何TDataSource的字段名。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43545441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档