有一个 TToolbar,它的值绑定到 TText.Text。
如果我更改 TToolbar 值,则 TText.Text 也会更改。到目前为止一切正常。
如果我手动设置 TToolbar 值,则 Binding 不会影响: TToolbar 值会更改,但 Text 不会。
有没有办法通过代码触发 LiveBinding?
当然,我可以手动设置 TToolbar.Value 和 TLabel.Text,如下面的代码中所述,但这意味着
- 由于设置 Label1.Text 的冗余代码容易失败
- 在更复杂的 LiveBinding 情况下不太舒服
示例 Delphi FMX 代码:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls, Data.Bind.EngExt, Fmx.Bind.DBEngExt,
System.Rtti, System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.Components, FMX.Objects;
type
TForm1 = class(TForm)
TrackBar1: TTrackBar;
Text1: TText;
Button1: TButton;
BindingsList1: TBindingsList;
LinkControlToPropertyText: TLinkControlToProperty;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
begin
TrackBar1.Value := 11;
// bad workaround:
// Text1.Text := Format('%n',[TrackBar1.Value]);
end;
end.
根据 Unit1.FMX:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop, iPhone, iPad]
DesignerMobile = False
DesignerWidth = 0
DesignerHeight = 0
DesignerDeviceName = ''
DesignerOrientation = 0
DesignerOSVersion = ''
object TrackBar1: TTrackBar
Frequency = 1.000000000000000000
Height = 20.000000000000000000
Orientation = Horizontal
Position.X = 32.000000000000000000
Position.Y = 64.000000000000000000
TabOrder = 1
Width = 100.000000000000000000
end
object Text1: TText
Height = 25.000000000000000000
Position.X = 152.000000000000000000
Position.Y = 64.000000000000000000
Width = 137.000000000000000000
end
object Button1: TButton
Height = 22.000000000000000000
Position.X = 32.000000000000000000
Position.Y = 96.000000000000000000
TabOrder = 3
Text = 'Button1'
Width = 80.000000000000000000
OnClick = Button1Click
end
object BindingsList1: TBindingsList
Methods = <>
OutputConverters = <>
Left = 20
Top = 5
object LinkControlToPropertyText: TLinkControlToProperty
Category = 'Schnelle Bindungen'
Control = TrackBar1
Track = False
Component = Text1
ComponentProperty = 'Text'
end
end
end