IDE:Flash Builder4.6
在开始之前先看一个例子,在使用<mx:DataGrid>类似标签时以下ItemRender的用法是可行的:
<mx:DataGrid>
<mx:columns>
<mx:DataGridColumn dataField="dataField1" headerText="姓名">
<mx:itemRenderer>
<mx:Component>
<mx:Label text="HA"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
现在把类似写法移植到4.6中:
<s:GridColumn dataField="dataField1" headerText="姓名">
<s:itemRenderer>
<fx:Component>
<s:Label text="HA"/>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
运行时报错:TypeError: Error #1009: Cannot access a property or method of a null object reference.
于是将写法改为:
主应用(GridItemRender.mxml)
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:VGroup width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<s:Panel width="80%" height="70%" title="ItemRender">
<s:DataGrid x="82" y="32" width="80%" height="70%" requestedRowCount="4">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="dataField1" headerText="姓名"></s:GridColumn>
<s:GridColumn dataField="dataField2" headerText="语文"></s:GridColumn>
<s:GridColumn dataField="dataField3" headerText="数学"></s:GridColumn>
<s:GridColumn dataField="dataField4" headerText=" " itemRenderer="com.itemRender.imageRender"></s:GridColumn>
</s:ArrayList>
</s:columns>
<s:ArrayList>
<fx:Object dataField1="Sam" dataField2="89" dataField3="80" dataField4="assets/wrong.gif"></fx:Object>
<fx:Object dataField1="Jack" dataField2="88" dataField3="100" dataField4="assets/right.gif"></fx:Object>
<fx:Object dataField1="Tom" dataField2="92" dataField3="80" dataField4="assets/wrong.gif"></fx:Object>
<fx:Object dataField1="Kate" dataField2="98" dataField3="79" dataField4="assets/wrong.gif"></fx:Object>
</s:ArrayList>
</s:DataGrid>
</s:Panel>
</s:VGroup>
</s:WindowedApplication>
MXML组件(imageRender.mxml)
注意:
代码:
<?xml version="1.0" encoding="utf-8"?>
<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:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:layout>
<s:HorizontalLayout verticalAlign="middle" horizontalAlign="center"/>
</s:layout>
<s:Image source="{data.dataField4}"/>
</s:GridItemRenderer>
运行结果正常显示: