I have a strange problem. Setting th开发者_StackOverflowe text property of a flex DateField makes the selectedDate property of that DateField go to null.
I need to set the text property so that I can use a particular format (DD-MMM-YYYY).
Whilst in the middle of typing my question, I did some more investigations and a bit of googling and finally came across the answer.
http://www.cubicleman.com/2008/02/12/flex-datefield-and-formats/
I spent the best part of a day trying to find the solution to this and had already googled a good bit so thought that it might be a good idea to put it on stackoverflow to make it more likely that someone else will find the solution in the future.
The answer, essentially, is as follows.
You need to use a combination of a date formatter and a label function to have the date showing correctly. Then, to be able to set the date programmatically without Flex "messing with your head", you need to set the parseFunction on the dateField to null.
Code:
<mx:Script>
<![CDATA[
private function doDateLabel(item:Date):String {
return dateFormatter.format(item);
}
]]>
</mx:Script>
<mx:DateFormatter id="dateFormatter" formatString="DD/MMM/YYYY" />
<mx:DateField id="begin"
showToday="true"
labelFunction="doDateLabel"
parseFunction="null"/>
InputTxt.selectedDate = DateField.stringToDate(InputTxt.text, "DD/MM/YYYY");
See AS Reference for DateField.stringToDate.
精彩评论