Spark ComboBox - 显示新项目的文本问题?

时间:2010-09-16 23:06:57

标签: flex flex4

我一直在玩s:ComboBox,并且通常很喜欢它们。一个细节让我疯狂 - 很可能是因为我对这个主题缺乏了解 - 如果我尝试在changeHandler中添加一个新项目到我的dataprovider(注册到change事件),ComboBox textInput的文本就会消失 - 虽然项目添加工作完美。有趣的是,如果通过单击按钮调用它,即相同的操作可以正常工作,即更改事件已经过处理,并且文本不会消失。以下是here中显示此

的一些代码
<?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中,同时将项目添加到数据提供程序,如果您只是输入新项目,则不会发生这种情况项目,然后按Enter键。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

好吧,我想myLB_changeHandler的内容上的callLater似乎可以解决问题,即

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]);
}