【发布时间】:2012-12-23 17:39:08
【问题描述】:
概述
我正在创建一个从 TCustomTreeView 派生的组件,我想添加一个在对象检查器中显示下拉列表 (paValueList?) 的属性。此列表必须基于附加到我的控件的另一个列表(TStrings)动态填充,例如它可以是来自 TComboBox、TListBox 或来自 TStringList 等的字符串。
虽然我遇到了一些问题,但我真的可以得到一些指导和建议。
代码布局
我已缩短代码以使其更易于阅读,但布局与我所拥有的基本相同。
我已将我的组件包分成两个(同一个项目组),Package1 包含组件代码(例如,我的组件源自 TCustomTreeView),Package2 包含注册程序和设计器单元(designide.dcp、DesignIntf、设计编辑器等)。
Package2 是我认为我需要添加属性编辑器的地方,该编辑器将用于从Package1 中的 TCustomTreeView 派生的组件。
包1
unit MyTreeViewUnit;
implementation
uses
...
Classes,
SysUtils;
type
TMyTreeView = class(TCustomTreeView)
private
FSomeList: TStringList; // property editor should be filled using this list
procedure SetSomeList(const Value: TStringList);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property SomeList: TStringList read FSomeList write SetSomeList;
end;
....
包2
unit MyTreeViewPropUnit;
implementation
uses
DesignIntf,
DesignEditors,
Classes;
type
TMyTreeViewProperty = class(TStringProperty)
public
function GetAttributes: TPropertyAttributes; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure Edit; override;
end;
implementation
uses
MyTreeViewUnit;
function TMyTreeViewProperty.GetAttributes: TPropertyAttributes;
begin
Result := [paValueList]; ?
end;
procedure TMyTreeViewProperty.GetValues(Proc: TGetStrProc);
begin
inherited;
// These cannot be added here!!
// This list should be populated based on SomeList found in Package1 - MyTreeViewUnit.pas
Proc('Item1');
Proc('Item2');
end;
procedure TMyTreeViewProperty.Edit;
begin
inherited;
// ?
end;
unit MyCompsRegister;
interface
uses
Classes;
procedure Register;
implementation
uses
MyTreeViewUnit;
MyTreeViewPropUnit;
procedure Register;
begin
RegisterComponents('MyTree', TMyTreeView);
RegisterPropertyEditor(TypeInfo(String), TMyTreeView 'Test', TMyTreeView); // does not seem to add to object inspector for the component TMyTreeView
end;
end.
问题
第一个问题是我不知道我所做的是否是正确的方法,我的组件已安装并且我可以毫无问题地使用它,尽管我的属性编辑器“测试”没有出现!
第二个问题是 GetValues 的填充方式。在线查看一些文章使我对迄今为止在填充属性编辑器等方面的内容有了基本的了解。尽管出于我的需要,无法以这种方式添加,在此示例中,我需要 GetValues 来填充基于在第一个单元中分配给 SomeList 的字符串(正如我之前所说的,FSomeList 可以是 TListBox)。
这与问题 2 相关,即如何让我的属性编辑器(在工作时)与我的树视图进行通信,以便我可以相应地填充它?
如果有人能给我一些指点,或者更好地指导我写一篇关于编写属性编辑器的好文章/指南,我将不胜感激。我在 Delphi.about、DelphiDabbler 等上阅读的内容对我来说并不容易理解和遵循(我很容易感到困惑和陷入困境)。
非常感谢!
【问题讨论】:
-
如果你有 DevExpress Vertical Grid 控件,你会发现它包含一个非常好的属性编辑器组件。我开始写我自己的一个,然后我意识到我已经有了一个非常好的版本。你可以在这里找到更多信息:devexpress.com/Products/VCL/ExVerticalGrid
-
您已为名为@987654330@ 的
TMyTreeView字符串属性注册了属性编辑器,但TMyTreeView在您发布的代码中没有Test属性。您希望属性编辑器准确地编辑什么? -
我也注意到了,应该是
'SomeList'而不是'Test' -
@KenWhite 阅读了我对 Cosmin Prund 的回答的评论,你会再次发现,像往常一样,我被一个问题困住了太久,并且对我手头的任务变得更加困惑。我真的需要学会停止跳入事物,而是坐下来,花时间了解信息和文档指南等。我似乎说服自己一切都会很容易做到,到那时我已经太迟了深层次的困惑和沮丧。我似乎无法始终保持清醒的头脑,看到并理解我所做的一切,但我会继续努力!
标签: delphi custom-controls delphi-xe