我一直在尝试制作一个测试对象,该对象具有对可视绑定表单可见的字符串属性。该组件使用适当的属性注册。使用 XE8 和 Firemonkey。
我可以通过使用隐藏显示元素选择它来让它显示在可视活页夹上,但是我不能让它将 Foo 字符串绑定到 TEdit 文本属性,只读(它什么都不做)将绑定到文本属性。
- 如何以默认的类可见性显示它
- 为什么 Foo 字符串不绑定到 text 属性
帮助文件在这方面有点薄。我想我在这里遗漏了一些非常简单的东西。
目标代码如下。
谢谢。
unit TestOBj;
interface
uses
System.Classes, System.SysUtils, System.StrUtils, Data.Bind.Components;
Type
TMyLBObject = Class (TComponent)
private
fFooString:String;
FReadOnly: Boolean;
procedure SetReadOnly(const Value: Boolean);
procedure SetFooString(const Value: String);
protected
function CanObserve(const ID: Integer): Boolean; override;
procedure ObserverAdded(const ID: Integer; const Observer: IObserver); override;
procedure ObserverToggle(const AObserver: IObserver; const Value: Boolean);
published
property Foo:String read FFooString write SetFooString;
property ReadOnly:Boolean read FReadOnly write SetReadOnly;
End;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Test', [TMyLBObject]);
end;
{ TMyLBObject }
function TMyLBObject.CanObserve(const ID: Integer): Boolean;
begin
Result := False;
if ID = TObserverMapping.EditLinkID then
Result := True;
if ID = TObserverMapping.ControlValueID then
Result := True;
end;
procedure TMyLBObject.ObserverAdded(const ID: Integer;
const Observer: IObserver);
begin
if ID = TObserverMapping.EditLinkID then
Observer.OnObserverToggle := ObserverToggle;
end;
procedure TMyLBObject.ObserverToggle(const AObserver: IObserver;
const Value: Boolean);
var
LEditLinkObserver: IEditLinkObserver;
LSaveReadOnly: Boolean;
begin
if Supports(AObserver, IEditLinkObserver, LEditLinkObserver) then
begin
if Value then
begin
LSaveReadOnly := ReadOnly;
if LEditLinkObserver.IsReadOnly then
ReadOnly := True;
FReadOnly := LSaveReadOnly;
end
else
if not (csDestroying in ComponentState) then
ReadOnly := FReadOnly;
end;
end;
procedure TMyLBObject.SetFooString(const Value: String);
begin
FFooString := Value;
end;
procedure TMyLBObject.SetReadOnly(const Value: Boolean);
begin
FReadOnly := Value;
end;
initialization
RegisterClass(TMyLBObject);
RegisterObservableMember(TArray<TClass>.Create(TMyLBObject), 'Foo', 'FMX');
finalization
UnRegisterObservableMember(TArray<TClass>.Create(TMyLBObject));
end.