动态改变DataGird行的颜色
MXML实现如下:
<?xml version="1.0" encoding="utf-8"?><mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ import mx.controls.Alert; override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number, color:uint, dataIndex:int):void { if(dataProvider!="" && dataIndex < dataProvider.length){ if(dataProvider[dataIndex].objectid != "0"){ color = 0xFF6600 } } super.drawRowBackground(s,rowIndex,y,height,color,dataIndex); } ]]> </mx:Script> <mx:columns><mx:DataGridColumn headerText="线路ID" dataField="id" textAlign="left"/><mx:DataGridColumn headerText="线路名称" dataField="name" textAlign="left"/> </mx:columns></mx:DataGrid>package{import flash.display.Sprite;import mx.collections.ArrayCollection;import mx.controls.DataGrid;public class RowColorDataGrid extends DataGrid{//用于设置颜色,参数: 当前item, rowIndex, dataIndex, 当前colorpublic var rowColorFunction:Function;//覆写drawRowBackground,目的是根据rowColorFunction返回颜色设置当前行override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number,color:uint, dataIndex:int):void{if(rowColorFunction != null){//从dataProvider中获取当前itemvar item:Object;if(dataIndex < dataProvider.length){item = dataProvider[dataIndex];}if(item){ //如果当前item存在,从rowColorFunction中获取行的颜色color = rowColorFunction(item, rowIndex, dataIndex, color);}}//调用父类方法设置当前行颜色super.drawRowBackground(s, rowIndex, y, height, color, dataIndex);}}}}