改变DataGrid单元格字体颜色

        DataGrid单元格的默认颜色是黑色的,但我们平常用时常常要改变单元格字体字体颜色。一条数据在状态大于零时显示为黑色,状态小于零时颜色为红色。以下是一个我遇到的问题,共享一下我的解决办法。

package com.palm.fontsColorDG
{
	import flash.display.Sprite;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;

	
	public class FontsColorDataGrid extends DataGrid
	{
		public var theProperty:String = "";
		/**判断单元格时使用的DataField**/
		private var _cDataField:Object=null;
		/**外部引用函数,与内置函数ControlFunc相似,通过一个值参数,
		 * 再根据自定义的判断规则,返回将显示的颜色值
		 * **/
		private var _controlFunc:Function=null;
		
		public function FontsColorDataGrid()
		{
			super();
		}
		
		override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number, color:uint, dataIndex:int):void
		{
			var arr:ArrayCollection = dataProvider as ArrayCollection;
			if(arr == null || theProperty == "")
				return ;
			if (dataIndex < arr.length) {
//				var item:Object = arr.getItemAt(dataIndex);
//				if(item[theProperty] == Project.STATE_LIXIANG_0)
//					color = 0xff0000;
//				else
//					color = 0x0000ff;
			}
			super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);
		}

		
		/**
		 * 设置行字体颜色
		 * */
		public function set ControlDataField(value:Object):void{
			_cDataField=value;
		}
		public function set ControlFunction(value:Function):void{
			_controlFunc=value;
		}
		override public function set dataProvider(value:Object):void{
			if(_cDataField!=null){
				for each(var item:Object in value){
					if(_controlFunc!=null)
						item.color=_controlFunc(item[_cDataField]);
					else
						item.color=ControlFunc(item[_cDataField]);
					
				}
			}
			super.dataProvider=value;
		}
		private function ControlFunc(data:Object):uint{
			if(data>0){
				return 0xff0000;
			}else if(data<0){
				return 0x0000ff;
			}
			return null;
		}
	}
}
package com.palm.fontsColorDG
{
	import mx.controls.dataGridClasses.DataGridItemRenderer;
	
	public class FontsColorDGIRenderer extends DataGridItemRenderer
	{
		public function FontsColorDGIRenderer()
		{
			super();
		}
		
		override public function set data(value:Object):void{
			this.setStyle("color",value.color);
			super.data=value;
		}
	}
}


	
		0){
					return 0x0066CC;
				}else if(value<0){
					return 0x333333;
				}
				return 0xdd0000;
			}
		]]>
	
	
		
			
			
			
			
		
	




你可能感兴趣的:(flex)