我正在开发一个组件。此组件具有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;我不希望它有一个DataSource属性。如何实现TSecondaryPathListItem.DataField属性,使其成为dropDown列表(在属性编辑器中),显示组件的DataSource.DataSet字段?
发布于 2017-04-21 15:31:33
您的DataSource和DataField属性位于不同的类中,因此您必须为DataField属性编写和注册一个自定义属性编辑器来将它们链接到一起。您可以使用Delphi的标准TDataFieldProperty类作为编辑器的基础。TDataFieldProperty通常在声明DataField属性的同一个类中查找DataSource: TDataSource属性(名称是可自定义的),但可以将其调整为从主组件检索TDataSource对象。
创建一个设计时包,用于requires 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.现在,您可以将设计时包安装到IDE中,并且您的DataField属性应该显示一个下拉列表,其中包含来自分配给组件的任何TDataSource的字段名。
https://stackoverflow.com/questions/43545441
复制相似问题