1

我有一个在组件中发布的集合,我希望能够在对象检查器中选择一个集合项,而无需使用保存项目索引的方法。我已经将类型的属性发布到项目(TCollectionItem),但在对象检查器中它显示为一个子组件,没有选择另一个选项的选项。我注册了一个编辑器,以便可以显示项目列表,但是在单击项目下拉框时会出错。以下是该问题的说明性摘录:

TMyCollection = class(TCollection)
end;

TMyCollectionItem = class(TCollectionItem)
end;

TMycomponent = class(TComponent)
published
property MyColection: TMyCollection;
property MyChosedItem: TCollectionItem; //< this need to be a list of TCollectionItem
end;

在属性编辑器下面

type
TMyItemProperty = class(TClassProperty)
public
  function GetAttributes: TPropertyAttributes; override;
  procedure GetValueList(List: TStrings); virtual;
  procedure GetValues(Proc: TGetStrProc); override;
  procedure SetValue(const Value: string); override;
end;


function TMyItemProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paValueList, paSortList, paMultiSelect];
end;

procedure TMyItemProperty.GetValueList(List: TStrings);
var
  Item: TMyCollectionItem;
  Items: TMyCollection;
  I: Integer;
begin
  Items := (GetComponent(0) as TMyComponent).MyColection;

  if Items <> nil then
    for I := 0 to Items.Count-1 do
      List.Add(Items.Items[I].GetNamePath); // Is this the problem ?
end;

procedure TMyItemProperty.GetValues(Proc: TGetStrProc);
var
  I: Integer;
  Values: TStringList;
begin
  Values := TStringList.Create;
  try
    GetValueList(Values);
    for I := 0 to Values.Count - 1 do
      Proc(Values[I]);
  finally
    Values.Free;
  end;
end;

procedure TMyItemProperty.SetValue(const Value: string);
begin
  inherited SetValue(Value);
end;

当我打开属性下拉框时出现错误:“无效类型转换”。

如何正确实现此属性的属性编辑器?

4

0 回答 0