data binding with as3
I have a bit of a problem (since I'm not used to binding with AS3), the thing is that I want to do this ki开发者_运维知识库nd of data binding:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:XML id="dp" source="countries_states_cities.xml" format="e4x" />
<mx:Form>
<mx:FormItem label="Country:">
<mx:ComboBox id="countryCB"
dataProvider="{dp.country}"
labelField="@name" />
</mx:FormItem>
<mx:FormItem label="State:">
<mx:ComboBox id="stateCB"
dataProvider="{countryCB.selectedItem.state}"
labelField="@name" />
</mx:FormItem>
<mx:FormItem label="City:">
<mx:ComboBox id="cityCB"
dataProvider="{stateCB.selectedItem.city}"
labelField="@name" />
</mx:FormItem>
</mx:Form>
but the ComboBoxes are being created dynamically with AS3, everything is working except I cant bind the second ComboBox dataProvider to be the XMLList in the first ComboBox's selectedItem.
I guess you need runtime binding. It is done with BindingUtils class, see this for example. As for your case, it must be something like
BindingUtils.bindProperty(secondCombobox, "dataProvider",
firstCombobox, "selectedItem");
Are you creating it dynamically based on data? If so, you could always use the Repeater component to repeat what you're trying to accomplish.
<mx:Repeater dataProvider="{someData}">
<mx:FormItem label="{data.label}">
<mx:ComboBox dataProvider="{data.stateCB.selectedItem.city}"
labelField="@name" />
</mx:FormItem>
</mx:Repeater>
Thanks, that is exactly what I was looking for, the code line that works for me is something like:
BindingUtils.bindProperty(comboBox, 'dataProvider', parentCB, {name: 'selectedItem', getter: function (host:ComboBox):XMLList {
var dp:XMLList;
if (host.selectedIndex >= 0) dp = host.selectedItem.children();
else dp = new XMLList();
return dp;
}}, true);
However, I haven't been able to access the "site" inside the function, only the "host". Basically what I'm trying to do is make the second ComboBox start with selectedIndex = -1
精彩评论