2011-10-20 50 views
4

我有一个对象,我们称之为控制器,它不是一个组件,它有一个启用的属性。我可以使用实时绑定来将其绑定到表单上某些组件的已启用属性吗?即,如果组件的启用属性发生更改,与其绑定的组件的相应启用属性也会相应设置?是否可以将(使用实时绑定)多个组件绑定到(非组件)对象?

我可以看到如果TController是一个TComponent后代如何做到这一点。我知道我可以将我的TController封装在一个组件中,但是我问是否有直接的方法来做到这一点?

回答

4

这可以用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

1

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

Introduction to LiveBindings in Delphi XE2