2012-02-08 50 views
1

我无法将spark的DropDownList的selectedIndex公共属性绑定到它在视图的演示模型中的原始源。无法将DropDownList的selectedIndex绑定到Bindable类的selectedIndex成员

为了复制这个问题尽可能少的行,我有两个视图和一个演示模型。代码如下。

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 
     } 
    } 
} 

调试应用程序我发现,无论从DropDownList中选择哪个项目,呈现模型中的selectedIndex始终保持指定的默认值。对于上面的示例代码,这是-1。

如何在演示模型中绑定selectedIndex,以便在所选项目DropDownList更改时进行适当更新?

回答

4

调试我发现的selectedIndex在 演示模型总是停留在我从DropDownList中选择分配 无论哪个项目的默认值应用。对于上面的 示例代码,这是-1。

根据您提供的代码,这是正确的。您已将pm.selectedIndex绑定到myDropDownList.selectedIndex。所以,每当pm.selectedIndex发生变化,myDropDownList.selectedIndex的值就会改变。

你还没有做的是将myDropDownList.selectedIndex绑定到pm.selectedIndex。因此,对myDropDownList.selectedIndex所做的任何更改都不会对pm.selectedIndex产生影响。最简单的方法,使这种“绑定”两种方式工作是使用简写MXML语法:

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

More information on that in the docs,其中也包括了“预Flex 4的”另类它是使用绑定标签:

<mx:Binding source="myDropDownList.selectedIndex" destination="pm.selectedIndex"/> 
相关问题