更改Datagrid中的背景颜色单元格(Flex 4.6)

时间:2013-02-01 19:04:47

标签: flex datagrid flex4 flexbuilder

我正在尝试在数据网格中为每个单元格的两种背景颜色之间切换。它会在双击特定单元格时更改其状态。

我想要的是用户能够在Datagrid中选择单个单元格,因此#FFFFFF颜色用于未选择的单元格,#CDCDCD用于选定的单元格。

我有一个ItenRenderer的代码:

    <?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">

    <fx:Script>
        <![CDATA[
            override public function prepare(hasBeenRecycled:Boolean):void {
                //lblData.text = data[column.dataField]
            }
        ]]>
    </fx:Script>

    <s:states>
        <s:State name="normal"/>
        <s:State name="selected"/>
    </s:states>

    <s:Label id="lblData" top="9" left="7" text="10" width="100%" height="100%" textAlign="center"/>
    <s:Rect width="100%" height="100%">
        <s:fill>
            <s:SolidColor color.selected="#CDCDCD" color.normal="#FFFFFF"/>
        </s:fill>
    </s:Rect>

</s:GridItemRenderer>

问题是由于rect组件隐藏了标签,因此永远不会显示标签。我怎么能做到这一点?

提前致谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

enter image description here

//应用

<?xml version="1.0" encoding="utf-8"?>
<s: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">
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;

        [Bindable]private var initDG:ArrayCollection = new ArrayCollection([
            {Artist:'01', Album:'Album 01', Year:'2008'},
            {Artist:'02', Album:'Album 02', Year:'2009'},
            {Artist:'03', Album:'Album 03', Year:'2007'},
            {Artist:'04', Album:'Album 04', Year:'2003'},
        ]);
    ]]>
</fx:Script>

<s:VGroup>
    <s:DataGrid id="myGrid" width="360" dataProvider="{initDG}">   
        <s:columns>
            <s:ArrayList>
                <s:GridColumn dataField="Artist" headerText="Artist" itemRenderer="com.dgcoloredcells.CellRenderer"/>
                <s:GridColumn dataField="Album" headerText="Album" itemRenderer="com.dgcoloredcells.CellRenderer"/>
                <s:GridColumn dataField="Year" headerText="Year" itemRenderer="com.dgcoloredcells.CellRenderer"/>
            </s:ArrayList>
        </s:columns>       
    </s:DataGrid> 
</s:VGroup>
</s:Application>

//渲染

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

        private var _listData:BaseListData;

        [Bindable]private var isSelected:Boolean = false;

        override public function set data( value:Object ) : void 
        {
            super.data = value;
            lblData.text = data[column.dataField];
        }

        [Bindable]public function get listData() : BaseListData
        {
            return _listData;
        }
        public function set listData( value:BaseListData ) : void
        {
            _listData = value;
        }

        private function onClick(evt:Event):void
        {
            isSelected = !isSelected;
        }
    ]]>
</fx:Script>

<s:Rect width="100%" height="100%">
    <s:fill>
        <s:SolidColor color="{isSelected ? 0xCDCDCD : 0xFFFFFF}"/>
    </s:fill>
</s:Rect>

<s:Label id="lblData" top="9" left="7" width="100%" height="100%" textAlign="center" doubleClickEnabled="true" doubleClick="onClick(event)"/>

</s:GridItemRenderer>

<强> //修改 在这里,您可以看到我的数据截图[column.dataField]问题:

enter image description here

这是相应的值对象

enter image description here

所以data [column.dataField] = data [“Artist”] =“01”