Jump to content
Serge_G

Creating a component with LinkPropertyToFieldText when using Visual Livebindings

Recommended Posts

Hi,

I wrote a component but when I use visual livebindings this one is linked as a bidirectionnal  LinkPropertyToFieldText.

The component (a test one to get the good link) is a sort of thermometer 

 

 

unit CThermometre;

interface

uses
  System.SysUtils, System.Classes, System.UITypes,
  FMX.Types, FMX.Controls, FMX.Objects,FMX.Graphics,
  Data.Bind.Components;

type
  [ObservableMembers('Progress')]
  TThermometre = class(TRectangle)
  private
    FProgress: integer;
    procedure SetProgress(const Value: integer);
    procedure ObserverToggle(const AObserver: IObserver; const Value: Boolean);
    { Déclarations privées }
  protected
    { Déclarations protégées }
    function CanObserve(const ID: Integer): Boolean; override;  { declaration is in System.Classes }
    procedure ObserverAdded(const ID: Integer; const Observer: IObserver); override; { declaration is in System.Classes }
    procedure OnResize;
  public
    constructor Create(AOwner: TComponent); override;
    function Paint: Boolean; reintroduce;
    { Déclarations publiques }
  published
    { Déclarations publiées }
    property Progress : integer read FProgress write SetProgress;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TThermometre]);
end;

{ TRectangle1 }

function TThermometre.CanObserve(const ID: Integer): Boolean;
begin
 case ID of
    TObserverMapping.EditLinkID, TObserverMapping.ControlValueID:
      Result := True;
  else
    Result := False;
  end;
end;

constructor TThermometre.create(AOwner: TComponent);
var pos : single;
    Brosse : TBrush;
begin
  if not(csloading in ComponentState) then
  begin
    inherited;
    width:=30;
    height:=110;
    Xradius:=10;
    Yradius:=10;
    FProgress:=50;
    pos:=0.5;
    if pos=1 then pos:=1-0.000001;
    Brosse:=TBrush.Create(TBrushKind.Gradient,Talphacolors.null);
    try
      Brosse.Gradient.StartPosition.X:=0.5;
      Brosse.Gradient.StartPosition.Y:=1;
      Brosse.Gradient.StopPosition.X:=0.5;
      Brosse.Gradient.StopPosition.Y:=0;
      Brosse.Gradient.Points.Clear;
      Brosse.Gradient.Points.Add;
      Brosse.Gradient.Points[0].Color:=Talphacolors.red;
      Brosse.Gradient.Points[0].Offset:=0;
      Brosse.Gradient.Points.Add;
      Brosse.Gradient.Points[1].Color:=Talphacolors.red;
      Brosse.Gradient.Points[1].Offset:=pos;
      Brosse.Gradient.Points.Add;
      Brosse.Gradient.Points[2].Color:=Talphacolors.null;
      Brosse.Gradient.Points[2].Offset:=pos+0.000001;
      Brosse.Gradient.Points.Add;
      Brosse.Gradient.Points[3].Color:=Talphacolors.null;
      Brosse.Gradient.Points[3].Offset:=1;
      Fill:=Brosse;
    finally
     Brosse.Free;
    end;
  end;
end;

procedure TThermometre.ObserverAdded(const ID: Integer;
  const Observer: IObserver);
begin
  if ID = TObserverMapping.EditLinkID then
    Observer.OnObserverToggle := ObserverToggle;
end;

procedure TThermometre.ObserverToggle(const AObserver: IObserver;
  const Value: Boolean);
var
  LEditLinkObserver: IEditLinkObserver;
begin
  if Value then
  begin
    if Supports(AObserver, IEditLinkObserver, LEditLinkObserver) then
      Enabled := not LEditLinkObserver.IsReadOnly;
  end
  else
    Enabled := True;
end;

procedure TThermometre.OnResize;
begin
Paint;
end;

function TThermometre.Paint: Boolean;
var pos : single;
begin
BeginUpDate;
pos:=(FProgress/100);
if pos=1 then pos:=1-0.000001;

Fill.Gradient.Points.Clear;
Fill.Gradient.Points.Add;
Fill.Gradient.Points[0].Color:=Talphacolors.red;
Fill.Gradient.Points[0].Offset:=0;
Fill.Gradient.Points.Add;
Fill.Gradient.Points[1].Color:=Talphacolors.red;
Fill.Gradient.Points[1].Offset:=pos;
Fill.Gradient.Points.Add;
Fill.Gradient.Points[2].Color:=Talphacolors.null;
Fill.Gradient.Points[2].Offset:=pos+0.000001;
Fill.Gradient.Points.Add;
Fill.Gradient.Points[3].Color:=Talphacolors.null;
Fill.Gradient.Points[3].Offset:=1;

EndUpdate;
result:=true;
end;

procedure TThermometre.SetProgress(const Value: integer);
begin
  if FProgress<>Value then
   begin
    FProgress := Value;
    Paint;
   end;
end;



initialization

Data.Bind.Components.RegisterObservableMember
  (TArray<TClass>.create(TThermometre), 'Progress', 'FMX');

finalization

Data.Bind.Components.UnregisterObservableMember
  (TArray<TClass>.create(TThermometre));
end.

Works fine but I was expecting a LinkPropertyToFieldText (more logic).

 

I suspect something is missing in the functions/ procedures ObserverAdded, ObserverToggle or  CanObserve.  Any clues ?

 

Capture_1.PNG

Capture_2.PNG

Edited by Serge_G
add pictures

Share this post


Link to post

Hi, this morning, digging deep in the source I found the solution.

My fault was in the use of

Data.Bind.Components.RegisterObservableMember

to have a LinkPropertyToFieldText link you have to use Data.Bind.Components.RegisterValuePropertyName

initialization

Data.Bind.Components.RegisterValuePropertyName
  (TArray<TClass>.create(TLThermo), 'Progress', 'FMX');


finalization
Data.Bind.Components.UnRegisterValuePropertyName
  (TArray<TClass>.create(TLThermo));

So, it's solved
 

 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×