动态改变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>

在需要加入的页面:
<comp:DataGrid id="dg" dataProvider="{comboData}" horizontalGridLines="true" width="100%" click="queryLineInfo(event)" height="65%" right="0"/>


as3 CLASS实现如下:
package
{
	import flash.display.Sprite;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;

	public class RowColorDataGrid extends DataGrid
	{
	//用于设置颜色,参数: 当前item, rowIndex, dataIndex, 当前color
		public 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中获取当前item
				var item:Object;
				if(dataIndex &lt; dataProvider.length)
				{
					item = dataProvider[dataIndex];
				}
				if(item)
				{ //如果当前item存在,从rowColorFunction中获取行的颜色
					color = rowColorFunction(item, rowIndex, dataIndex, color);
				}
				}
				//调用父类方法设置当前行颜色
				super.drawRowBackground(s, rowIndex, y, height, color, dataIndex);
			}
		}
	}
}

你可能感兴趣的:(xml,Flash)