我的数据绑定有一些问题,希望有人能帮助我。
我为我想要实现的目标创建了一个非常简单的示例,您可以在下面看到。
<?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 时,视图不会更新。
存在2个问题:
当我从 _list.dataProvider 中删除 'children()' 时,ComboBox 使用绑定进行更新,但我需要读取子项,因此绑定失败。
尽管模型被定义为可绑定的,但文本永远不会更新。
为什么绑定到 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() 对我来说很重要。
这可能吗?如果没有,有谁知道我将如何解决这个问题?
对此的任何建议将不胜感激。