使用实时绑定,是否可以将多个组件绑定到一个非组件对象上?

4
我有一个对象,叫做Controller,它不是一个组件,但它有一个enabled属性。我能否使用实时绑定将其绑定到表单上某些组件的enabled属性?也就是说,如果我的组件的enabled属性发生变化,那么与之绑定的组件的enabled属性也会相应地设置?
如果TController是TComponent的后代,我知道如何做到这一点。我知道我可以将我的TController包装在一个组件中,但我想知道是否有直接的方法来实现这个功能?
2个回答

5
这可以通过使用BindScope实现:BindScope1.DataObject := Controller;
  object BindingsList1: TBindingsList
    Methods = <>
    OutputConverters = <>
    UseAppManager = True
    Left = 464
    Top = 56
    object BindExpression1: TBindExpression
      Category = 'Binding Expressions'
      ControlComponent = Label1
      SourceComponent = BindScope1
      SourceExpression = 'enabled'
      ControlExpression = 'visible'
      NotifyOutputs = False
      Direction = dirSourceToControl
    end
    object BindExpression2: TBindExpression
      Category = 'Binding Expressions'
      ControlComponent = CheckBox1
      SourceComponent = BindScope1
      SourceExpression = 'enabled'
      ControlExpression = 'IsChecked'
      NotifyOutputs = False
      Direction = dirSourceToControl
    end
  end

TBindings.Notify(Controller, 'Enabled');会评估所有相关的表达式。在上面的示例中,这些表达式是Label1.Visible和Checkbox1.IsChecked。


2

几天前,我在我们当地的Be-Delphi会议上介绍了Delphi XE2中的LiveBindings。我在我的网站上发布了幻灯片和完整的“白皮书”或“教程”文档。在最后一章中,我尝试解释如何将组件绑定到在运行时创建的TPerson实例。也许对你有一些有趣的信息:

Delphi XE2中LiveBindings的介绍


链接已失效,但可以在以下网址找到其材料:https://web.archive.org/web/20190622014700/http://www.devia.be/news/article/introduction-to-livebindings-in-delphi-xe2/ - https://www.slideshare.net/StefaanLesage/bedelphi-livebindings-in-delphi-xe2-devia - https://www.scribd.com/document/73106743/Be-Delphi-Live-Bindings(可能自那时以来LiveBindings已经进化了)。 - George Birbilis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接