我无法将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更改时进行适当更新?