您可以通过它的 owner 属性访问它:
(this.owner as DataGrid).selectedIndex
调度自定义事件的简单 itemRenderer:
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
override public function prepare(hasBeenRecycled:Boolean):void {
lblData.text = data[column.dataField]
}
protected function onClickButton(ev:MouseEvent):void {
trace("ButtonDispatchItemRenderer::onClickButton");
//dispatch the Event which has a static const GET_SELECTED_INDEX for the event type
this.owner.dispatchEvent(new MyEvent(MyEvent.GET_SELECTED_INDEX));
}
]]>
</fx:Script>
<s:HGroup verticalAlign="baseline">
<s:Label id="lblData" top="9" left="7"/>
<s:Button label="dispatch" click="onClickButton(event)"/>
</s:HGroup>
</s:GridItemRenderer>
带有监听事件的 DataGrid 的简单应用程序
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600" creationComplete="onCreationComplete()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import spark.components.gridClasses.GridColumn;
[Bindable]
public static var initDG:ArrayCollection = new ArrayCollection([
{label:"firstItem"}, {label:"secondItem"}, {label:"thirdItem"}
]);
protected function onCreationComplete():void {
dataGrid.addEventListener(MyEvent.GET_SELECTED_INDEX, onRendererButtonClick, false, 0, true);
}
protected function onRendererButtonClick(ev:MyEvent):void {
trace("Application::RenderButtonClick");
}
]]>
</fx:Script>
<s:VGroup>
<s:DataGrid id="dataGrid" verticalCenter="0" horizontalCenter="0" dataProvider="{initDG}">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="label" itemRenderer="renderers.ButtonDispatchItemRenderer"></s:GridColumn>
</s:ArrayList>
</s:columns>
</s:DataGrid>
</s:VGroup>
</mx:Application>
您必须向包含 selectedIndex 的事件添加一个属性。你必须将值传递给渲染器内的事件(例如构造函数中的第二个参数)这个例子只是调度事件