我一直在玩s:ComboBox,一般很喜歡它們。但是有一個細節讓我吃了一驚 - 很可能是因爲我對主題缺乏瞭解 - 如果我嘗試在changeHandler(註冊到change事件)中向我的數據提供者添加新項目,ComboBox textInput的文本消失 - 雖然項目增加完美。有趣的是,如果通過點擊按鈕來調用相同的操作,即處理了更改事件後,並且文本不會消失,那麼該操作就可以正常工作。下面是here一些代碼,顯示了這個Spark組合框 - 顯示新項目文本的問題?
<?xml version="1.0" encoding="utf-8"?>
<!-- dpcontrols\spark\SparkCBAddItem.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:layout>
<s:VerticalLayout paddingTop="5" paddingLeft="5"/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import spark.events.IndexChangeEvent;
[Bindable]
public var myDP:ArrayList = new ArrayList(["Red", "Orange", "Yellow", "Blue", "Green"]);
// Event handler to determine if the selected item is new.
protected function myCB_changeHandler(event:IndexChangeEvent):void
{
// Determine if the index specifies a new data item.
if(myCB.selectedIndex == spark.components.ComboBox.CUSTOM_SELECTED_ITEM)
{
// Add the new item to the data provider.
myCB.dataProvider.addItem(myCB.selectedItem);
}
}
protected function button1_clickHandler(event:MouseEvent):void
{
// Determine if the index specifies a new data item.
if(myCB.selectedIndex == spark.components.ComboBox.CUSTOM_SELECTED_ITEM)
{
// Add the new item to the data provider.
myCB.dataProvider.addItem(myCB.selectedItem);
}
}
]]>
</fx:Script>
<s:Label text="The selected index is: {myCB.selectedIndex}"/>
<s:Label text="The selected item is: {myCB.selectedItem}"/>
<s:ComboBox id="myCB" width="140" change="myCB_changeHandler(event);" dataProvider="{myDP}"/>
<s:Button label="Button" click="button1_clickHandler(event)"/>
</s:Application>
如果剔除變更處理程序,你會看到,添加一個新的項目,然後點擊按鈕不斷TextInput中的新元素,同時增加該項目的數據提供者,如果您只輸入一個新項目並按回車則不會發生。
在此先感謝您的幫助!
即使你處理它與輸入的事件嗎?我認爲那一刻CUSTOM_SELECTED_ITEM出了問題,但是後來的調用看起來足夠好,但是。讓我知道。 – Eugene 2010-09-19 17:17:06