2010-09-16 46 views
0

我一直在玩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中的新元素,同時增加該項目的數據提供者,如果您只輸入一個新項目並按回車則不會發生。

在此先感謝您的幫助!

+0

即使你處理它與輸入的事件嗎?我認爲那一刻CUSTOM_SELECTED_ITEM出了問題,但是後來的調用看起來足夠好,但是。讓我知道。 – Eugene 2010-09-19 17:17:06

回答

2

好吧,我想使用callLater上myCB_changeHandler的內容,似乎這樣的伎倆,即

private function later(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); 
    } 

} 

// Event handler to determine if the selected item is new. 
protected function myCB_changeHandler(event:IndexChangeEvent):void 
{ 
     callLater(later, [event]); 
} 
+0

爲我工作。我在更改事件時調用了一些代碼,並將焦點放在了另一個組件上,並且它將保持打開下拉列表。之後使用通話它就像一個魅力。 – 2016-02-08 10:53:54