如何在flex 4中的datagrid中访问项呈示器中的listData属性

时间:2012-10-28 14:46:29

标签: flex itemrenderer

我有一个自定义数据网格。我通过属性标准将hslider的值传递给它。我有一个用于此datagrid的外部itemrenderer,它扩展了GridItemRenderer。我想根据此属性条件动态更改项呈示器。要从父组件访问属性'标准',我想访问listData属性。为此,这个自定义数据网格实现了IDropinItemRender。但是listData的值变为null。在我的主应用程序中,我正在使用此数据网格,如下所示。

<components:Mydatagrid dataProvider="{ac}" criteria="{hslider.value}">
<components:columns>
        <s:ArrayList>
            <s:GridColumn dataField="name" headerText="Name"></s:GridColumn>
            <s:GridColumn dataField="age" headerText="Age"></s:GridColumn>
            <s:GridColumn dataField="hair" headerText="Hair" itemRenderer="renderer.GridInlineDynamicRenderer"   />
        </s:ArrayList>
    </components:columns>
</components:Mydatagrid>

这是我的项目渲染器代码。 set data函数的第2行中的listData属性为null。

<?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" 
                clipAndEnableScrolling="true" implements="mx.controls.listClasses.IDropInListItemRenderer" >
<fx:Script>
    <![CDATA[
        import mx.controls.dataGridClasses.DataGridListData;
        import mx.controls.listClasses.BaseListData;

        import components.Mydatagrid;
        private var _listData:BaseListData;
        public function get listData() : BaseListData
        {
            return _listData;
        }
        public function set listData( value:BaseListData ) : void
        {
            _listData = value;
        }
        protected function button1_clickHandler(event:MouseEvent):void
        {
            dispatchEvent(new Event('hairEvent',true));
        }
        override public function set data(value:Object):void
        {
            super.data = value;
            var myListData:DataGridListData = DataGridListData(listData);
            var criteria:Number = (myListData.owner as Mydatagrid).criteria;
            hairClr.setStyle('color',data.hair);
            if(data.age < criteria)
                alpha = 0.4;
            else
                alpha = 1;
        }
    ]]>
</fx:Script>

<mx:HBox> 
    <s:Button click="button1_clickHandler(event)" label="Button" />
    <s:Label id="hairClr" text="Color" />
</mx:HBox>

</s:GridItemRenderer>

listData在此处为null。但是当我使用项目渲染器和列表组件时,它可以工作。项目渲染器的代码是相同的,除了列表项目渲染器扩展HBox。此代码的链接是http://www.adobe.com/devnet/flex/articles/itemrenderers_pt3.html。我只是想用datagrid实现它。

<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" implements="mx.controls.listClasses.IDropInListItemRenderer">
<fx:Script>
    <![CDATA[
        import mx.controls.listClasses.BaseListData;

        import components.MyList;

        private var _listData:BaseListData;
        public function get listData() : BaseListData
        {
            return _listData;
        }
        public function set listData( value:BaseListData ) : void
        {
            _listData = value;
        }
        protected function button1_clickHandler(event:MouseEvent):void
        {
            dispatchEvent(new Event('hairEvent',true));
        }
        override public function set data(value:Object):void
        {
            super.data = value;
            var criteria:Number = (listData.owner as MyList).criteria;
            hairClr.setStyle('color',data.hair);
            if(data.age < criteria)
                alpha = 0.4;
            else
                alpha = 1;
        }
    ]]>
</fx:Script>

    <s:Button click="button1_clickHandler(event)" label="Button" />
    <s:Label id="hairClr" text="Color" />
</mx:HBox>

0 个答案:

没有答案