Flex3中的ComboBox selectedItem

时间:2009-10-15 08:36:24

标签: flex actionscript-3 flex3 air

我正在使用Flex3中的Air应用程序,我需要知道如何设置“selectedItem”属性,当我们有2个值(数据和标签)标签属性到组合框选择,我们输入的数据值。

如下所示。

                                   

在(selectedItem =“{stylename}”中,stylename将具有“data”值,但我需要将“lable”属性设置为组合框中的选定值。

如果stylename被“选中”,那么ComboBox所选项目需要“已检查”。

如何在flex中实现它....

先谢谢

1 个答案:

答案 0 :(得分:2)

ComboBox.selectedItem正在寻找Object。你传递的是String字面值。 “stylename”在哪里设置?如果这来自外部源,您可以检索要在setter函数中选择的项目:

ActionScript 3:

[Bindable]
public var comboBoxData:ArrayCollection;

[Bindable]
private var comboBoxSelectedItem:Object = {};

private var _styleName;

private function get styleName():String
{
    return _styleName;
}

private function set styleName(value:String):void
{
    _styleName = value;

    comboBoxSelectedItem = getItemFromCollection("styleName", value);
}

private function getItemFromCollection(property:String, value:String):Object
{
    // Create a copy of the Collection used as the dataProvider for the ComboBox
    var filteredCollection:ArrayCollection = 
        new ArrayCollection(comboBoxData.toArray());

    // Set a filterFunction to filter only those Objects with the specified name/value pair
    filteredCollection.filterFunction = 
        function(item:Object):Boolean
        {
            return item[property] == value;
        }

    // Refresh the collection to apply the filterFunction
    filteredCollection.refresh();

    // Return an empty Object if no Object was found with the given name/value pair
    if (filteredCollection.length == 0)
        return {};

    // Return the first/only Object in the filtered Collection
    return filteredCollection.getItemAt(0);
}

MXML:

<mx:ComboBox dataProvider="{comboBoxData}" selectedItem="{comboBoxSelectedItem}" />