【问题标题】:Delphi - Populate Property editor dropdown with list?Delphi - 使用列表填充属性编辑器下拉列表?
【发布时间】:2017-09-18 14:49:17
【问题描述】:

我正在开发一个组件。该组件具有 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 属性,使其成为下拉列表(在属性编辑器中),显示组件的 DataSource.DataSet 字段?

【问题讨论】:

    标签: delphi properties components cascadingdropdown


    【解决方案1】:

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

    创建一个设计时包,其中包含requires IDE 的designidedcldb 包,以及组件的运行时包。实现一个派生自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 的字段名称。

    【讨论】:

      猜你喜欢
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多