首页 > 设计 > WEB开发 > 正文

14.5.链式的属性绑定

2023-07-22 12:39:04
字体:
来源:转载
供稿:网友
14.5.1 问题
我想定义一个源属性做为一个链式属性中的一部分。
14.5.2. 解决办法
使用<mx:Binding>标签或者花括号,通过使用点连接方式存取一个属性链中的属性,或者在静态方法BindingUtils.bindProperty 和BindingUtils.bindSetter 使用链式的字符串数组做为参数来进行处理。
14.5.3. 讨论
当一个源属性被在数据绑定表达式中定义,则所有的属性的更改都会监视。如果你指定了TextInput 控件的text 属性做为绑定的数据源,则TextInput 控件实例则是可绑定的属性链中的一部分。
+展开
-XML
<mx:TextInput id="myInput" />
<mx:Label text="{myInput.text}" />

技术上,myInput 控件所在的类也是属性绑定链中的一部分,但是数据绑定语句是有可见性,所以该指向是没有必要。本质上,myInput 的值首先被设置为非空,同时,绑定也移动到了链中的源:TextInput 实例中的text 属性。只有当源数据被绑定时,才会触发将数据复制到目的对象的事件。

在上一章节的例子中,我们是存取同一个模块中的一个数据链,就像源属性来自于同一个控制器。在MXML 中,你可以通过使用点标记的语法定义一个可以绑定的数据链。
+展开
-XML
<!-- property chain binding using <mx:Binding> -->
<mx:Binding source="usermodel.name.firstName"
destination="fNameField.text" />

<mx:Label id="fNameField" />
<!-- property chain binding using curly braces ({}). -->
<mx:Label text="{usermodel.name.firstName}" />

在ActionScript 3 为了定义一个可以绑定的数据链,你必须指定这个链为一个字符串值的数组,不管是你调用BindingUtils.bindProperty 或者BindingUtils.bindSette 方法:
+展开
-ActionScript
BindingUtils.bindProperty( nameField, "text",usermodel, ["name""firstName"] );
BindingUtils.bindSetter( invalidateProperties, this, ["usermodel""name""firstName"] );

这两个方法的参数是一个字符串的数组,其中定义了绑定的数据链:

接下来的例子中,使用了花括号、<mx:Binding>标记,以及方法BindingUtils.bindProperty 来使用数据链来进行数据绑定。
+展开
-XML
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxmllayout="verticalcreationComplete="initHandler();">
<mx:Script>
<![CDATA[
import mx.binding.utils.BindingUtils;
// create data binding using BindingUtils.
private function initHandler():void
{
BindingUtils.bindProperty( lastNameField, "text", usermodel, ["name""lastName"] );
}
private function clickHandler():void
{
usermodel.name.firstName = fNameInput.text;
usermodel.name.lastName = fNameInput.text;
usermodel.birth.date = dateInput.text;
}

]]>
</mx:Script>
<!-- defined model -->
<mx:Model id="usermodel">
<user>
<name>
<firstName>Ted</firstName>
<lastName>Henderson</lastName>
</name>
<birth>
<date>February 29th, 1967</date>
</birth>
</user>
</mx:Model>
<!-- create data binding using <mx:Binding> -->
<mx:Binding source="usermodel.birth.date"
destination="dateField.text" />

<mx:Form>
<mx:FormItem label="First Name:">
<!-- create data binding using curly braces -->
<mx:Text text="{usermodel.name.firstName}" />
</mx:FormItem>
<mx:FormItem label="Last Name:">
<mx:Text id="lastNameField" />
</mx:FormItem>
<mx:FormItem label="Birthday:">
<mx:Text id="dateField" />
</mx:FormItem>
</mx:Form>
<mx:HRule />
<mx:Form>
<mx:FormItem label="First Name:">
<mx:TextInput id="fNameInput" />
</mx:FormItem>
<mx:FormItem label="Last Name:">
<mx:TextInput id="lNameInput" />
</mx:FormItem>
<mx:FormItem label="Birthday:">
<mx:TextInput id="dateInput" />
</mx:FormItem>
<mx:FormItem label="Submit Changes">
<mx:Button label="okclick="clickHandler();" />
</mx:FormItem>
</mx:Form>
</mx:Application>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表