【发布时间】:2012-02-08 03:43:37
【问题描述】:
我无法将 spark DropDownList 的 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 更改时适当更新?
【问题讨论】: