使用DateField做为ItemEditor

使用DateField做为ItemRenderer或ItemEditor时,如果输入源是String类型时,使用原始的DateField不能达到目的.所以需要扩展一下:

<?xml version="1.0" encoding="utf-8"?>
<mx:DateField xmlns:mx="http://www.adobe.com/2006/mxml"
	editable="true" open="openHandler()" close="closeHandler()">
    <mx:Script>
       <![CDATA[
       	import mx.controls.listClasses.ListData;
       	import mx.controls.Alert;
       	import mx.controls.dataGridClasses.DataGridListData;
       	
       	private var _selectedDate:Date = null;
       	
       	private var _textFormatString:String = "YYYYMMDD";
       	
       	public function set textFormatString(value:String):void 
       	{
       		if(value) _textFormatString = value;
       	}
       	
       	public function get textFormatString():String 
       	{
       		return _textFormatString;
       	}
       	
       	override public function set formatString(value:String):void 
       	{
       		if(value == null) {
       			super.formatString = "YYYY.MM.DD";
       		} else {
       			super.formatString = value;
       		}
       	}
       	
       	override public function get formatString():String 
       	{
       		return super.formatString || "YYYY.MM.DD";
       	}
       	
	    override public function set data(value:Object):void
	    {
	    	var newDate:Date;
	    	
            if (value is String)
            {
                newDate = DateField.stringToDate(value as String, textFormatString);
            }
            else if (value is Date)
            {
                newDate = value as Date;
            } 
            else if(value != null) 
            {
            	var v:Object;
                if (listData && listData is DataGridListData)
                {
                	v = value[DataGridListData(listData).dataField];
		            newDate = v is Date ? v as Date : DateField.stringToDate(v as String, textFormatString);
                }
		        else if (listData is ListData && ListData(listData).labelField in value)
		            v = value[ListData(listData).labelField];
		            newDate = v is Date ? v as Date : DateField.stringToDate(v as String, textFormatString);
            }
	            
	        this.selectedDate = newDate;
	    }
	    
	    override public function get text():String 
	    {
	    	return DateField.dateToString(this.selectedDate, textFormatString);
	    }
	    
	    override public function set text(value:String):void
	    {
	    	super.text = value; Alert.show(value);
	    }
	    
	    override  public function get selectedDate():Date
	    {
	        return _selectedDate;
	    }
	    
	    override public function set selectedDate(value:Date):void
	    {
	    	super.selectedDate = value;
	    	this._selectedDate = value;
	    }
	    
	    private function openHandler():void {
	    	super.open();
	    	super.selectedDate = this.selectedDate;
	    }
	    
	    private function closeHandler():void {
	    	super.close();
	    	this.selectedDate = super.selectedDate;
	    }
	    
       ]]>
    </mx:Script>
</mx:DateField>

 

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