<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" creationcomplete = "Init ()">
<Mx: SCRIPT>
<! [CDATA [
Import MX. Collections. xmllistcollection;
Import MX. Collections. arraycollection;
/*
* This example highlights several common bindings to ComboBox, including arrays, XML-based data, and mx: Model-Based binding.
For XML, we usually need to locate the xmllist corresponding to children () to bind
*/
[Bindable]
Private var listdata: arraycollection;
[Bindable]
Private var xmldata: xmllistcollection;
[Bindable]
Private var xmlnode: xmllistcollection;
[Bindable]
Private var xmllist: xmllist;
Private function Init (): void
{
// Array-based binding
VaR list: array = [{label: 'China', value: 'cn'}, {label: 'u.s. ', value: 'usa'}, {label: 'uk ', value: 'uk'}];
// Binding Based on XML Attribute Definitions
This. listdata = new arraycollection (list );
VaR XML: xml = <root>
<Item label = "DataGrid" value = "1"/>
<Item label = "ComboBox" value = "2"/>
<Item label = "list" value = "3"/>
</Root>;
// Construct an XML node to define xmllistcollection
Xmldata = new xmllistcollection (New xmllist ([XML]);
VaR xml1: xml = <root>
<Item>
<Label> JAVA </label>
<Value> A </value>
</Item>
<Item>
<Label> C # </label>
<Value> B </value>
</Item>
<Item>
<Label> Delphi </label>
<Value> C </value>
</Item>
</Root>;
Xmlnode = new xmllistcollection (New xmllist ([xml1]);
// Construct an xmllist object directly
Xmllist = new xmllist ();
Xmllist + = <item> <label> White </label> <value> White </value> </item>;
Xmllist + = <item> <label> black </label> <value> black </value> </item>
}
Private function binddata (): void
{
If (cbbindtype. selecteditem. value = "xml1 "){
Cblist. dataprovider = xmldata. Children ();
Cblist. labelfield = "@ label ";
Datagridcolumn (grid1.columns [0]). datafield = "@ label ";
Datagridcolumn (grid1.columns [1]). datafield = "@ value ";
}
Else if (cbbindtype. selecteditem. value = "xml2 "){
Cblist. dataprovider = xmlnode. Children ();
Cblist. labelfield = "label ";
Datagridcolumn (grid1.columns [0]). datafield = "label ";
Datagridcolumn (grid1.columns [1]). datafield = "value ";
}
Else if (cbbindtype. selecteditem. value = "xml3 "){
Cblist. dataprovider = xmllist;
Cblist. labelfield = "label ";
Datagridcolumn (grid1.columns [0]). datafield = "label ";
Datagridcolumn (grid1.columns [1]). datafield = "value ";
}
Else {
Cblist. dataprovider = listdata;
Cblist. labelfield = "label ";
Datagridcolumn (grid1.columns [0]). datafield = "label ";
Datagridcolumn (grid1.columns [1]). datafield = "value ";
}
Grid1.dataprovider = cblist. dataprovider;
Cbvaluechanged ();
}
Private function cbvaluechanged (): void {
VaR item: Object = cblist. selecteditem;
If (item is XML ){
If (cbbindtype. selecteditem. value = "xml1 ")
Tiprompt. Text = XML (item). @ value;
Else
Tiprompt. Text = XML (item). value;
}
Else {
Tiprompt. Text = item. value;
}
}
]>
</MX: SCRIPT>
<Mx: Model id = "mtype">
<Root>
<Item label = "XML property binding" value = "xml1"/>
<Item label = "XML Node Binding" value = "xml2"/>
<Item label = "xmllist binding" value = "xml3"/>
<Item label = "array binding" value = "array"/>
</Root>
</MX: Model>
<Mx: ComboBox x = "52" Y = "20" id = "cbbindtype" dataprovider = "{mtype. Item}" labelfield = "label" width = "152">
</MX: ComboBox>
<Mx: ComboBox x = "52" Y = "70" id = "cblist" width = "152" change = "cbvaluechanged ()">
</MX: ComboBox>
<Mx: Label x = "9" Y = "22" text = "binding type"/>
<Mx: button x = "236" Y = "20" label = "bind" Click = "binddata ()"/>
<Mx: textinput x = "52" Y = "100" id = "tiprompt"/>
<Mx: DataGrid id = "grid1" Height = "107" x = "236" Y = "69" width = "240">
<Mx: columns>
<Mx: datagridcolumn headertext = "label" datafield = "label"/>
<Mx: datagridcolumn headertext = "value" datafield = "value"/>
</MX: columns>
</MX: DataGrid>
</MX: Application>