Delphi XE2 e LiveBindings tra i controlli
-
27-10-2019 - |
Domanda
È possibile eseguire il vivo tra i controlli, cioè prendere 2 caselle di modifica e ottenere il risultato di aggiungere il loro contenuto insieme in un'etichetta. Sono sicuro che lo sia, non so da dove cominciare
Grazie
Soluzione
Dai un'occhiata ai campioni. URL repository SVN: https://radstudiodemos.svn.sourceforge.net/svnroot/radstudiodemos/branches/radstudio_xe2/livebindings
Un esempio:
------ unità1.dfm -----
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 286
ClientWidth = 426
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 8
Top = 62
Width = 48
Height = 13
Caption = 'Edit1Edit2'
end
object Edit1: TEdit
Left = 8
Top = 8
Width = 121
Height = 21
TabOrder = 0
Text = 'Edit1'
OnChange = EditChange
end
object Edit2: TEdit
Left = 8
Top = 35
Width = 121
Height = 21
TabOrder = 1
Text = 'Edit2'
OnChange = EditChange
end
object BindingsList1: TBindingsList
Methods = <>
OutputConverters = <>
UseAppManager = True
Left = 20
Top = 5
object BindExpressionLabel11: TBindExpression
Category = 'Binding Expressions'
ControlComponent = Label1
SourceComponent = BindScope1
SourceExpression = 'Edit1.Text + Edit2.Text'
ControlExpression = 'Caption'
NotifyOutputs = False
Direction = dirSourceToControl
end
end
object BindScope1: TBindScope
Left = 192
Top = 16
end
end
----- unità1.pas -----
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.Bind.EngExt, Vcl.Bind.DBEngExt,
System.Rtti, System.Bindings.Outputs, Vcl.Bind.Editors, Data.Bind.Components,
Vcl.StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
BindingsList1: TBindingsList;
BindExpressionLabel11: TBindExpression;
BindScope1: TBindScope;
procedure EditChange(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
System.Bindings.Helper;
procedure TForm1.EditChange(Sender: TObject);
begin
TBindings.Notify(Sender, 'Text');
end;
end.
Come utilizzare IDE Designer per produrre il risultato:
- Metti due modifiche (EDIT1, EDIT2), un'etichetta (etichetta) e un tbindscope (bindscope1) nel modulo (forma1).
- Crea un gestore di eventi per l'evento OnChange di entrambe le modifiche (Editchange).
- Seleziona etichetta1, espandi il menu a discesa della proprietà LiveBindings, seleziona "Nuovo legame live ...", seleziona tbindexpression
- Modifica proprietà della nuova creazione BindExpressionLabel11: Assegna la didascalia a Controlexpression, BindScope1 a SourceComponent, EDIT1.Text + Edit2.Text a SourceSpression
Altri suggerimenti
Il progetto di esempio nella posizione (predefinita) di:
C:\Users\Public\Documents\RAD Studio\9.0\Samples\Delphi\LiveBinding\Components\bindexpression\fmx\BindExpressionSampleProject.dproj
lo fa proprio questo.
Non è necessario che tbindScope per leghino i componenti insieme. Supponi di avere EDIT1 e EDIT2 nel modulo. Se si imposta EDIT2 BINGINSOURCE su EDIT1, sarà link alle modifiche a EDIT1