简体   繁体   English

flex ComboBox的双向绑定?

[英]Bidirectional binding for flex ComboBox?

I have a collection that I want to bind as data input for a ComboBox : 我有一个要绑定为ComboBox数据输入的集合:

private static var LOGOS:ArrayCollection = new ArrayCollection([
 {index:0, label=logo1}, 
 {index:1, label=logo2}
]);

<s:ComboBox selectedItem="@{model.labelIndex}" labelField="@label" dataProvider="{LOGOS}" />

Now, when selecting an item, the binding should send the associated index property of the objext to the model and update labelIndex . 现在,当选择一个项目时,绑定应将labelIndex的关联index属性发送到模型并更新labelIndex Of course it does not work as above, because labelIndex is of different datatype than the ArrayCollection . 当然,它不能如上所述工作,因为labelIndex的数据类型与ArrayCollection数据类型不同。

[Bindable]
private var model:MyModel;

[Bindable]
class MyModel {
   public var:Number labelIndex;
}

Question: how can I map the array element to the model and vice versa? 问题:如何将数组元素映射到模型,反之亦然?

What you are looking for will require some scripting, binding isn't smart enough to figure out how to handle this on its own. 您要查找的内容将需要一些脚本,绑定不够聪明,无法弄清楚如何自行处理。

You can use the BindingUtils class to define the bindings, and use the chain argument of the bindProperty method to modify how values are being looked up. 您可以使用BindingUtils类来定义绑定,并使用bindProperty方法的chain参数来修改查找值的方式。

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/binding/utils/BindingUtils.html http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/binding/utils/BindingUtils.html

For the combo.selectedItem to model.labelIndex binding, you can specify the chain as an array, where the elements define the path where to look for the value: 对于combo.selectedItemmodel.labelIndex绑定,您可以将链指定为数组,其中元素定义查找值的路径:

BindingUtils.bindProperty(model, 'labelIndex', combo, ['selectedItem', 'index']);

This will bind to the selectedItem property, and pass the value of the items index property. 这将绑定到selectedItem属性,并传递items index属性的值。

The other way around is a little more tricky and will require using a getter function which grabs the object from the datasource, based on the labelIndex value: 另一种方法则比较棘手,将需要使用getter函数,该函数根据labelIndex值从数据源中获取对象:

BindingUtils.bindProperty(combo, 'selectedItem', model, {
    name: 'labelIndex',
    getter: function(host:MyModel):Object
    {
        return LOGOS.source.filter(function(item:Object, index:int, array:Array):Boolean
        {
            return item.index === host.labelIndex;
        })[0];
    }
});

This will bind to the labelIndex property, and the getter function will be invoked when the property changes. 这将绑定到labelIndex属性,并且在属性更改时将调用getter函数。 The function will filter the datasource based on the models changed labelIndex property value, and return the source object with the matching index property value, which will finally be set for the combobox selectedItem property. 该函数将根据模型更改后的labelIndex属性值过滤数据源,并返回具有匹配index属性值的源对象,该属性最终将为组合框selectedItem属性设置。

Your combobox definition will of course need an id in order to be targetable via script 您的组合框定义当然需要一个id才能通过脚本进行定位

<s:ComboBox id="combo" dataProvider="{LOGOS}" />

Note that there's no need forthe the @ in the labelField property, this is only used with XML datasources where you need to target an attribute. 请注意,在labelField属性中不需要@ ,它仅用于需要定位属性的XML数据源。 However, actually you don't need to specify this at all, since label is the default value of the labelField property. 但是,实际上,您根本不需要指定它,因为labellabelField属性的默认值。

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

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