简体   繁体   English

Flex:有没有办法将ComboBox的selectedItem绑定到变量?

[英]Flex: Is there a way to bind ComboBox's selectedItem to a variable?

OK I have a ComboBox, the dataProvider is an array of objects with label properties that give the ComboBox the list of options. 好的,我有一个ComboBox,dataProvider是具有标签属性的对象数组,这些标签属性为ComboBox提供了选项列表。

Is there a way I can have a variable like mySelectedItem, and bind the ComboBox's selectedItem to it so that if it changes, the ComboBox's selectedItem will change to whatever it is? 有没有一种方法可以让我拥有mySelectedItem之类的变量,并将ComboBox的selectedItem绑定到该变量,以便如果它发生更改,ComboBox的selectedItem也将更改为它的任何值?

I hope this makes sense. 我希望这是有道理的。

Thanks! 谢谢!

Yes, ComboBox's selectedItem property is bindable. 是的,ComboBox的selectedItem属性是可绑定的。

It would go something like this: 它会像这样:

<mx:ComboBox selectedItem="{mySelectedItem}">
</mx:ComboBox>

In your AS: 在您的AS中:

[Bindable]
var mySelectedItem:Object;

Changes to mySelectedItem should show up in ComboBox. 对mySelectedItem的更改应显示在ComboBox中。 You may get errors if the item referenced by mySelectedItem does not exist in the ComboBox's dataProvider. 如果ComboBox的dataProvider中不存在mySelectedItem引用的项目,则可能会出错。

On the surface, it's as simple as: 从表面上看,它很简单:

<mx:ComboBox id="myComboBox"
   dataProvider="{myDataProvider}"
   selectedItem="{defaultItem}"/> 

When you set defaultItem (make sure it is [Bindable]) to one of the items in the data provider, it will update the control. 当将defaultItem(确保它是[Bindable])设置为数据提供程序中的一项时,它将更新控件。

But there are problems with this approach. 但是这种方法存在问题。 Unless currentDefaultItem always changes AFTER myDataProvider, the binding to dataProvider may undo the selection, reverting to the default (first item in the list). 除非currentDefaultItem在myDataProvider之后始终更改,否则与dataProvider的绑定可能会撤消选择,并恢复为默认值(列表中的第一项)。

One way around this is to force selectedItem to be rebound after dataProvider, by including dataProvider in the call providing the selectedItem. 解决此问题的一种方法是通过将dataProvider包含在提供selectedItem的调用中,强制将selectedItem反弹到dataProvider之后。

<mx:ComboBox id="myComboBox"
   dataProvider="{myDataProvider}"
   selectedItem="{getSelectedItem(myComboBox.dataProvider, defaultItem)}"/>

What this does is ensure selectedItem will be rebound when either currentDefaultItem changes, or after the dataProvider changes. 这是为了确保当currentDefaultItem更改时或dataProvider更改后,selectedItem将被反弹。 I'd be interested in other solutions myself. 我自己会对其他解决方案感兴趣。

Use an event listener for the Change event and do your processing there. 将事件侦听器用于Change事件并在那里进行处理。

// update a label item's text with that of the Combobox's selectedItem
private function changeEvt(event:Event):void {
    label.text =event.currentTarget.selectedItem.label + " " + 
}

or, you could do something like this if you don't mind extending ComboBox; 或者,如果您不介意扩展ComboBox,则可以执行以下操作: This is pseudocode (sorry, identification of matches depends on the object type) - but you get the idea... 这是伪代码(对不起,匹配的标识取决于对象类型)-但您会明白...

public class IndexRetainingComboBox extends ComboBox 
{
    public function IndexRetainingComboBox()
    {
        super();
    }

    override public function set dataProvider(value:Object):void
    {
        var originalSelection:Object = this.selectedItem;
        super.dataProvider = value;
        var newIdx:uint = [find originalSelection idx in combobox or return 0 ]
        this.selectedIndex = newIdx;
    }
}

I know this is how its described in the documentation. 我知道这是文档中描述的方式。 As in a change to the selectedItem will fire the change listener. 就像对selectedItem的更改一样,它将触发更改侦听器。 However for me, this does not happen. 但是对我来说,这不会发生。 Anyone else encounter the same behavior? 还有其他人遇到相同的行为吗?

这看起来很不错:将value属性设置为可写: http : //flex.sys-con.com/node/312098

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM