2012-02-08 8 views
1

spark DropDownListのselectedIndex publicプロパティをビューのプレゼンテーションモデルの元のソースにバインドできません。DropDownListのselectedIndexをBindableクラスのselectedIndexメンバーにバインドできません

できるだけ少ない行でこの問題を再現するために、2つのビューと1つのプレゼンテーションモデルがあります。コードは次のとおりです。

Main.mxml

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" 
       minWidth="955" 
       minHeight="600" 
       xmlns:views="com.blah.views.*"> 

    <views:DropDownListView/> 

</s:Application> 

DropDownListView.mxml

<?xml version="1.0" encoding="utf-8"?> 
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"> 

    <fx:Script> 
     <![CDATA[ 
      import com.blah.presentationmodels.DropDownListPresentationModel; 

      [Bindable] 
      private var pm:DropDownListPresentationModel = new DropDownListPresentationModel(); 
     ]]> 
    </fx:Script> 

    <s:DropDownList id="myDropDownList" 
        dataProvider="{pm.list}" 
        selectedIndex="{pm.selectedIndex}" 
        valueCommit="{pm.selectionChanged()}"/> 

</s:Group> 

DropDownListPresentationModel.as

package com.blah.presentationmodels 
{ 
    import mx.collections.ArrayCollection; 

    [Bindable] 
    public class DropDownListPresentationModel 
    { 
     public var selectedIndex:int = -1; 
     public var list:ArrayCollection = new ArrayCollection(); 

     public function DropDownListPresentationModel() 
     { 
      list.addItem("Load of Bread"); 
      list.addItem("Stick of Butter"); 
      list.addItem("Carton of Milk"); 
     } 

     public function selectionChanged():void 
     { 
      var newSelectedIndex:int = selectedIndex; // always -1 
     } 
    } 
} 

アプリケーションのデバッグプレゼンテーションモデルのselectedIndexは、DropDownListから選択したアイテムに関係なく、常にデフォルト値のままです。上記のサンプルコードでは-1です。

プレゼンテーションモデルでselectedIndexをバインドすると、選択したアイテムDropDownListが変更されたときに適切に更新されます。私は プレゼンテーションモデル内のselectedIndexは関係なく、常に私がDropDownListコントロールから選択された項目の を割り当てられたデフォルト値のままであることを見つけるアプリケーションのデバッグ

答えて

4

。上記の サンプルコードの場合、-1です。

これは、提供したコードに基づいて正しいです。 pm.selectedIndexをmyDropDownList.selectedIndexにバインドしました。したがって、pm.selectedIndexが変更されるたびに、myDropDownList.selectedIndexの値が変更されます。

あなたが行っていないことは、myDropDownList.selectedIndexからpm.selectedIndexにバインドされています。したがって、myDropDownList.selectedIndexの変更は、pm.selectedIndexにはまったく影響しません。

:また、結合タグを使用することです「前のFlex 4」の代替を含め

<s:DropDownList id="myDropDownList" 
       selectedIndex="@{pm.selectedIndex}" /> 

More information on that in the docs、この「結合」の仕事に両方の方法を作るための最も簡単な方法は、速記MXMLシンタックスを使用することです

<mx:Binding source="myDropDownList.selectedIndex" destination="pm.selectedIndex"/> 
関連する問題