0

データバインディングに問題があります。誰かが助けてくれることを願っています。

私が達成しようとしていることの非常に簡単な例を作成しました。これを以下に示します。

    <?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 つの問題が存在します。

  1. _list.dataProvider から「children()」を削除すると、ComboBox はバインディングを使用して更新されますが、子を読み取る必要があるため、バインディングは失敗します。

  2. モデルがバインド可能として定義されているにもかかわらず、テキストは決して更新されません。

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() にバインドすることが重要です。

これは可能ですか?そうでない場合、この問題を回避する方法を誰かが知っていますか?

これに関するアドバイスは大歓迎です。

4

1 に答える 1

0

これは、ゲッターとセッターを使用する必要がある場所です。例えば:

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
  }
}
于 2010-09-09T17:41:07.037 に答える