Flex将ComboBox绑定到XML子级

时间:2010-09-09 17:31:36

标签: xml flex binding combobox children

我的数据绑定存在一些问题,希望有人能帮助我。

我为我想要实现的目标创建了一个非常简单的示例,您可以在下面看到。

    <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center" verticalAlign="middle" initialize="init()">

    <!-- Controller -->
    <mx:Script>
        <![CDATA[
                import mx.collections.XMLListCollection;
                import mx.binding.utils.BindingUtils;

                protected var _tally:Number = 3;

                //RAW XML
                [Bindable]protected var _model:XML = new XML("<model><option title='Option 1'/> <option title='Option 2'/> <option title='Option 3'/> </model>");

                //This should bind the children to the XMLLList BUT DOES NOT
                [Bindable]protected var _list:XMLList = new XMLList(_model.children());

                //This Binds the _list to the _collection
                [Bindable]protected var _collection:XMLListCollection = new XMLListCollection(_list);


                //ADDS NEW DATA TO MODEL
                protected function updateModel():void 
                {
                    _tally++; 
                    _model.appendChild(new XML("<option title='Option " + _tally + "'/>"))
                    trace(_model)
                }

        ]]>
    </mx:Script>

    <!-- View -->
    <mx:Panel title="Combo Binding Test" >
        <mx:ComboBox id="_combo" width="100%" labelField="@title" dataProvider="{_collection}" />
        <mx:Text id="_text" height="100" width="300" selectable="false" text="{_model}" />
        <mx:ApplicationControlBar width="100%" dock="true">
            <mx:Button label="Update Model" click="updateModel()" />
        </mx:ApplicationControlBar>
    </mx:Panel>

</mx:Application>

(我希望格式化好!)

当我预览这个时,我可以看到绑定已将数据放入正确的位置,但是当我用更多数据更新XML时,视图不会更新。

存在两个问题:

  1. 当我从_list.dataProvider中删除'children()'时,ComboBox会使用绑定进行更新,但我需要读取子节点,因此绑定失败。

  2. 尽管模型被定义为可绑定,但文本永远不会更新。

  3. 为什么绑定到children()?

    我创建了一个自定义组件,它将从其父级接收不同的数据集。在这个自定义组件中有一个需要显示数据子节点的ComboBox。如果我无法绑定到子节点,我可能必须对每个使用它的实例进行硬编码。

    例如,一旦数据实例可能是:

    <locations>
    <option title="Hampshire"/>
    <option title="Warwickshire"/>
    <option title="Yorkshire"/>
    </locations> 
    

    另一个可能是:

    <stock>
    <option title="Hammer"/>
    <option title="Drill"/>
    <option title="Spanner"/>
    </stock>
    

    因此,绑定到children()对我很重要。

    这是否可行,如果没有,有谁知道我将如何解决这个问题?

    对此有任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是你应该使用getter和setter的地方。例如:

private var _list:XMLList;

[Bindable]
public function get list() : XMLList {
  return _list;
}

public function set list(value:XMLList) : void {
  _list = value;
  // also can try list.refresh() here if this doesn't do the job
}

然后在commitProperties(或creationComplete,或其他)中执行以下操作:

override protected function commitProperties() : void {
  super.commitProperties();
  if (_model && _model.children.length > 0) {
    list = _model.children(); // or use E4X as _model..option
  }
}
相关问题