[size=large]绑定到数组中的元素[/size]
可以使用数组中的单个元素作为数据绑定源,如下例所示:
<?xml version="1.0"?>
<!-- binding/ArrayBinding.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var myAC:ArrayCollection = new ArrayCollection([
"One", "Two", "Three", "Four"]);
[Bindable]
public var myAC2:ArrayCollection = new ArrayCollection([
"Uno", "Dos", "Tres", "Quatro"]);
]]>
</mx:Script>
<!-- Data binding updated at application startup
and when myAC modified. -->
<mx:Text id="text1" text="{myAC[0]}"/>
<!-- Data binding updated at application startup,
when myAC modified, and when myAC[0] modified. -->
<mx:Text id="text2" text="{myAC.getItemAt(0)}"/>
<mx:Button id="button1"
label="Change Element"
click="myAC[0]='new One'"/>
<mx:Button id="button2"
label="Change ArrayCollection"
click="myAC=myAC2"/>
</mx:Application>
[color=darkred]如果通过方括号语法[]来指定数组元素作为数据绑定表达式的源,那么数据绑定只在应用
启动时触发,或者在数组或其引用被更新时触发。当这个数组元素被更新的时候不会触发数据
绑定。
但数据绑定表达式中的myAC.getItemAt(0)则会在该数组元素变化时被触发更新。[/color]因此,id
为 text2 的Text 控件在点击button1 时会被更新,而id 为text1 的Text 控件则不会被更新。
当使用数组中的元素作为数据绑定表示的源时,应当在绑定表达式中使用
ArrayCollection.getItemAt()方法。
可以使用数组中的单个元素作为数据绑定源,如下例所示:
<?xml version="1.0"?>
<!-- binding/ArrayBinding.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var myAC:ArrayCollection = new ArrayCollection([
"One", "Two", "Three", "Four"]);
[Bindable]
public var myAC2:ArrayCollection = new ArrayCollection([
"Uno", "Dos", "Tres", "Quatro"]);
]]>
</mx:Script>
<!-- Data binding updated at application startup
and when myAC modified. -->
<mx:Text id="text1" text="{myAC[0]}"/>
<!-- Data binding updated at application startup,
when myAC modified, and when myAC[0] modified. -->
<mx:Text id="text2" text="{myAC.getItemAt(0)}"/>
<mx:Button id="button1"
label="Change Element"
click="myAC[0]='new One'"/>
<mx:Button id="button2"
label="Change ArrayCollection"
click="myAC=myAC2"/>
</mx:Application>
[color=darkred]如果通过方括号语法[]来指定数组元素作为数据绑定表达式的源,那么数据绑定只在应用
启动时触发,或者在数组或其引用被更新时触发。当这个数组元素被更新的时候不会触发数据
绑定。
但数据绑定表达式中的myAC.getItemAt(0)则会在该数组元素变化时被触发更新。[/color]因此,id
为 text2 的Text 控件在点击button1 时会被更新,而id 为text1 的Text 控件则不会被更新。
当使用数组中的元素作为数据绑定表示的源时,应当在绑定表达式中使用
ArrayCollection.getItemAt()方法。