FLEX4实践—DataGrid嵌入ItemRender

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>

运行结果正常显示:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值