Flex组件之间传值问题解决方法
用事件来解决,我这里分三个文件:Main.mxml、ComponentA.mxml、ComponentB.mxml内容分别是:
Main.mxml文件
=======================
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init();" xmlns:local="*">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
internal function init():void{
comA.addEventListener(ListEvent.ITEM_CLICK,componentA_itemClick);
}
internal function componentA_itemClick(evt:ListEvent):void{
comB.text=evt.columnIndex.toString();
}
]]>
</mx:Script>
<local:ComponentA id="comA" width="100%" height="100%" />
<local:ComponentB id="comB" width="100%" height="30" />
</mx:Application>
ComponentA.mxml文件
============================
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
private function dg_itemClick(evt:ListEvent):void{
//转发此事件
this.dispatchEvent(evt);
}
]]>
</mx:Script>
<mx:DataGrid width="100%" height="100%" itemClick="dg_itemClick(event);">
<mx:columns>
<mx:DataGridColumn headerText="测试1" dataField="@c1" />
<mx:DataGridColumn headerText="测试2" dataField="@c2" />
</mx:columns>
<mx:dataProvider>
<mx:XMLList xmlns="">
<item c1="aaaa" c2="aaaaa" />
<item c1="bbbb" c2="bbbbb" />
</mx:XMLList>
</mx:dataProvider>
</mx:DataGrid>
</mx:VBox>
componentB.mxml文件
==========================
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
<mx:Script>
<![CDATA[
public function set text(value:String):void{
lab.text=value;
}
]]>
</mx:Script>
<mx:Label id="lab" />
</mx:HBox>