gpt4 book ai didi

Delphi:使组件对实时绑定(bind)可见

转载 作者:行者123 更新时间:2023-12-03 15:54:09 25 4
gpt4 key购买 nike

我一直在尝试制作一个具有对视觉绑定(bind)表单可见的字符串属性的测试对象。该组件已使用适当的属性进行注册。使用XE8和Firemonkey。

我可以通过使用隐藏显示元素选择它来将其显示在可视绑定(bind)器上,但是我无法让它将 Foo 字符串绑定(bind)到 TEdit 文本属性,只读(尚未执行任何操作)将绑定(bind)到文本属性。

  • 如何让它以默认类可见性显示在其中
  • 为什么 Foo 字符串不绑定(bind)到文本属性

帮助文件对此的描述有点薄弱。我想我在这里错过了一些非常简单的东西。

目标代码如下。

谢谢。

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.

最佳答案

您的 TMyLBObject 组件没有 [ObservableMember('Foo')] 属性。

type
[ObservableMember('Foo')] // <-- add this
TMyLBObject = Class (TComponent)
...
published
property Foo:String read FFooString write SetFooString;
...
End;

请参阅文档中的示例:

Tutorial: Creating LiveBindings-Enabled Components .

其中包括此注释:

The ObservableMember attribute will be used by LiveBindings components to generate expressions. RegisterObserverableMember() will be used at design time to inform the LiveBindings Designer.

关于Delphi:使组件对实时绑定(bind)可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31963141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com