自定义DataGrid的ItemRenderer

自定义的ItemRenderer要实现IDataRenderer和IDropInListItemRenderer,其中IDataRenderer里有get/set data方法,IDropInListItemRenderer里有get/set listData方法,下面定义的ItemRenderer支持labelFunction函数。

package component
{
	
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.controls.Text;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridBase;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridListData;
	import mx.controls.listClasses.AdvancedListBase;
	import mx.controls.listClasses.BaseListData;
	import mx.controls.listClasses.IDropInListItemRenderer;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.IDataRenderer;
	import mx.core.UIComponentGlobals;
	import mx.core.mx_internal;
	import mx.events.FlexEvent;
	
	
	use namespace mx_internal;
	
	
	public class TradeOpStatusDGColumnItemRenderer extends Label implements IDataRenderer, IDropInListItemRenderer	
{
		
		private var _data:Object;
		private var _listData:AdvancedDataGridListData;
		public var contentBackgroundColor:uint;
		
		public function TradeOpStatusDGColumnItemRenderer()
		{
			super();
		}
			
		[Bindable("dataChange")]
		override public function get data():Object
		{
			return _data;
		}
			
		override public function set data(value:Object):void
		{
			_data = value;
			var operationStatus:String;
			if(value!=null){
				if(value.hasOwnProperty("operationStatus")){
					operationStatus=value.operationStatus;
				}else{
					Alert.show("No such filed:operationStatus","Message");
					return;
				}
				setStyle("color","#000000");
				setStyle("borderVisible","false");
				if(operationStatus ==TradeOperationStatus.Added){
					setStyle("fontWeight","bold");
					setStyle("fontStyle","normal");
				}else if(operationStatus == TradeOperationStatus.Modified){
					setStyle("fontWeight","bold");
					setStyle("fontStyle","normal");
				}else if(operationStatus==TradeOperationStatus.Deleted){
					setStyle("fontWeight","bold");
					setStyle("fontStyle","italic");
				}else if(operationStatus ==TradeOperationStatus.Historical){
					setStyle("fontWeight","normal");
					setStyle("fontStyle","normal");
				}else{
					Alert.show("Trade operation status is not supported","Message");
					return;
				}
				
				if(_listData && _listData.dataField=="notional"){
					setStyle("textAlign","right");
				}else{
					setStyle("textAlign","left");
				}
				
if(_listData && _listData.dataField){
					var dataGrid:AdvancedDataGrid=_listData.owner as AdvancedDataGrid;
					var dataGridColumn:AdvancedDataGridColumn=dataGrid.columns[_listData.columnIndex] as AdvancedDataGridColumn
					super.text=dataGridColumn.itemToLabel(_data,true);
				}			
}
			dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
		}	
		
		[Bindable("dataChange")]
		override public function get listData():BaseListData
		{
			return _listData;
		}
		
		override public function set listData(value:BaseListData):void
		{
			_listData = AdvancedDataGridListData(value);
		}
			
		override public function drawFocus(isFocused:Boolean):void
		{
			
		}
	}
}


这里有一点要特别注意的地方就是,setStyle()方法设置的样式属性必须是你继承的组件里面有的,比如我这里是Label,那么你用setStyle("fontWeight","bold");setStyle("fontStyle","normal")都没问题,但是如果你用setStyle("lineThrough","true")就不行了,因为lineThrough只要s|TextInput组件有,mx|TextInput,和这里的Label没有,不过如果你设置了,运行时是不会报错的,只是你看不到你期待的结果而已。

你可能感兴趣的:(datagrid,Flex,column,自定义,itemRenderer)