【问题标题】:Unable to bind the selectedIndex of a DropDownList to the selectedIndex member of a Bindable class无法将 DropDownList 的 selectedIndex 绑定到 Bindable 类的 selectedIndex 成员
【发布时间】: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 更改时适当更新?

【问题讨论】:

    标签: apache-flex data-binding


    【解决方案1】:

    调试应用程序我发现 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,其中还包括使用绑定标签的“pre-Flex 4”替代方案:

    <mx:Binding source="myDropDownList.selectedIndex" destination="pm.selectedIndex"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-23
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多