无法通过代码设置LiveBinding值

时间:2014-07-25 09:34:26

标签: delphi firemonkey livebindings

有一个TToolbar,它的值绑定到TText.Text 如果我更改TToolbar值,TText.Text也会更改。 到目前为止一切正常 如果我设置TToolbar值手册,则绑定不会影响: 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

1 个答案:

答案 0 :(得分:7)

不幸的是,由于设计的原因,绑定不是由Value属性的setter触发,而是由GUI交互(鼠标点击或按键)触发。

您可以通过向OnAssigningValue绑定添加TLinkControlToProperty事件处理程序,然后在触发时查看callstack来查看。

要手动触发,您需要写:

TBindings.Notify(TrackBar1, 'Value');

为此,您必须为您的使用添加System.Bindings.Helper

编辑:您可以使用可视实时绑定创建的绑定似乎无法使用手动通知(如何一致...),您必须使用其他方法:

TLinkObservers.ControlChanged(TrackBar1);