4

有一个 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
4

1 回答 1

10

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

您可以通过将OnAssigningValue事件处理程序添加到您的TLinkControlToProperty绑定然后在它触发时查看调用堆栈来看到这一点。

要手动触发此操作,您需要编写:

TBindings.Notify(TrackBar1, 'Value');

为此,您必须添加System.Bindings.Helper到您的用途中。

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

TLinkObservers.ControlChanged(TrackBar1);      
于 2014-07-25T10:00:59.230 回答