7

我创建了一个从 TCustomPanel 派生的组件。在那个面板上,我有一个从 TOwnedCollection 派生的类的已发布属性。一切正常,在对象检查器中单击该属性的省略号会打开默认集合编辑器,我可以在其中管理列表中的 TCollectionItems。

  TMyCustomPanel = class(TCustomPanel)
  private
  ...
  published
    property MyOwnedCollection: TMyOwnedCollection read GetMyOwnedCollection write SetMyOwnedCollection;
  end;

我还希望能够在设计时双击面板并默认打开集合编辑器。我首先创建了一个派生自 TDefaultEditor 的类并注册它。

  TMyCustomPanelEditor = class(TDefaultEditor)
  protected
    procedure EditProperty(const PropertyEditor: IProperty; var Continue: Boolean); override;
  end;

  RegisterComponentEditor(TMyCustomPanel, TMyCustomPanelEditor);

这似乎在正确的时间运行,但我被困在当时如何启动集合的属性编辑器。

procedure TMyCustomPanelEditor.EditProperty(const PropertyEditor: IProperty; var Continue: Boolean);
begin
  inherited;

  // Comes in here on double-click of the panel
  // How to launch collection editor here for property MyOwnedCollection?

  Continue := false;
end;

任何解决方案或不同的方法将不胜感激。

4

2 回答 2

10

据我所知,您没有使用正确的编辑器。TDefaultEditor是这样描述的:

一个为双击提供默认行为的编辑器,它将遍历属性,寻找最合适的方法属性进行编辑

这是一个编辑器,它通过将您放入带有新创建的事件处理程序的代码编辑器来响应表单上的双击。想一想当您双击 aTButton并进入OnClick处理程序时会发生什么。

很久没有写设计时编辑器了(我希望我的记忆今天还有效),但我相信你的编辑器应该源自TComponentEditor. 为了显示您ShowCollectionEditor从该ColnEdit单元调用的集合编辑器。

您可以覆盖的Edit方法TComponentEditorShowCollectionEditor从那里调用。如果你想更高级,作为替代方案,你可以用GetVerbCount,GetVerb和声明一些动词ExecuteVerb。如果你这样做,那么你扩展上下文菜单,默认Edit实现将执行动词 0。

于 2011-09-16T15:12:44.483 回答
5

按照大卫的正确答案,我想提供完整的代码,在设计时双击 UI 控件的特定属性时显示 CollectionEditor。

type
  TMyCustomPanel = class(TCustomPanel)
  private
  ...
  published
    property MyOwnedCollection: TMyOwnedCollection read GetMyOwnedCollection write SetMyOwnedCollection;
  end;


  TMyCustomPanelEditor = class(TComponentEditor)
  public
    function GetVerbCount: Integer; override;
    function GetVerb(Index: Integer): string; override;
    procedure ExecuteVerb(Index: Integer); override;
  end;


procedure Register;
begin
  RegisterComponentEditor(TMyCustomPanel, TMyCustomPanelEditor);
end;

function TMyCustomPanelEditor.GetVerbCount: Integer;
begin
  Result := 1;
end;

function TMyCustomPanelEditor.GetVerb(Index: Integer): string;
begin
  Result := '';
  case Index of
    0: Result := 'Edit MyOwnedCollection';
  end;
end;

procedure TMyCustomPanelEditor.ExecuteVerb(Index: Integer);
begin
  inherited;
  case Index of
    0: begin
          // Procedure in the unit ColnEdit.pas
          ShowCollectionEditor(Designer, Component, TMyCustomPanel(Component).MyOwnedCollection, 'MyOwnedCollection');
       end;
  end;
end;
于 2011-09-18T14:06:18.527 回答