【问题标题】:Flex 3 Binding problemFlex 3 绑定问题
【发布时间】:2010-12-25 14:49:01
【问题描述】:

我有一个自定义的 ActionScript 类:

package EntityClasses
{
import mx.collections.ArrayCollection;


[RemoteClass(alias="tkatva.tt.entities.CompanyInfo")]
[Bindable]
public class CompanyInfo
{
        public var companyInfoId:int;
        public var companyName:String;
        public var streetAddress:String;
        public var postNumber:String;
        public var city:String;
        public var country:String;
        public var email:String;
        public var businessIdentityCode:String;
        public var intBic:String;
        public var homepageUrl:String;
        public var lastUpdatedBy:String;
        public var lastUpdateDate:Date;
        public var version:int;
        public var settingGroupList:ArrayCollection;
        public var bankAccountList:ArrayCollection;
        public var personList:ArrayCollection;


}
}

我想将这些值绑定到文本框,以便当用户向文本框键入信息时,它也会填充到类中。 Flex 3 是双向的,以便用 [Bindable] 标记类我可以将值绑定到文本框吗?

这是我尝试在其中绑定类的 mxml 文件:

    import mx.rpc.events.ResultEvent;
        import EntityClasses.CompanyInfo;

        [Bindable]
        public var company:CompanyInfo;



        public  var uuid:String;

        private function init():void {
            company = new CompanyInfo();
        }

        private function getCompanyInfo():void {
            try {
                company = TtRo.getCompanyInfo(uuid);

            } catch (Exception) {

            }
        }

        private function handleClick():void {
            this.txtInfo.text = "COMPANY:" + company.companyName;


             TtRo.addCompanyInfo(company,uuid);

        }

        private function handleAdding(e:ResultEvent):void {
            var res:Boolean;
            res = e.result as Boolean;
            if (res) {
                this.txtInfo.text = "OK";
            } else {
                this.txtInfo.text = "NOTOK";
            }
        }

    ]]>
</mx:Script>
<mx:TextInput x="194" y="10" id="txtCname" text="{company.companyName}"/>
<mx:TextInput x="194" y="40" id="txtStreet" text="{company.streetAddress}"/>
<mx:TextInput x="194" y="70" id="txtPostnumber" text="{company.postNumber}"/>
<mx:TextInput x="194" y="100" id="txtCity" text="{company.city}"/>
<mx:TextInput x="194" y="130" id="txtCountry" text="{company.country}"/>
<mx:TextInput x="194" y="160" id="txtEmail" text="{company.email}"/>
<mx:TextInput x="194" y="190" id="txtBic" text="{company.businessIdentityCode}"/>
<mx:TextInput x="194" y="220" id="txtIntBic" text="{company.intBic}"/>
<mx:TextInput x="194" y="250" id="txtUrl" text="{company.homepageUrl}"/>

这有什么问题吗? flex 编译器向我显示了这种警告:数据绑定将无法检测到“公司”的分配。

我是 Flex 的新手,如有任何帮助,将不胜感激...谢谢...

【问题讨论】:

    标签: apache-flex data-binding binding bindable


    【解决方案1】:

    不,Flex 数据绑定不是双向的。您必须使用BindingUtils.bindProperty() 将文本字段的text 属性显式绑定回company 对象的相应属性

    private function init():void 
    {
        company = new CompanyInfo();
        BindingUtils.bindProperty(company, "companyName", this ["txtCname", "text"]);
    }
    

    【讨论】:

      【解决方案2】:

      如果您在 MXML 组件中使用它,如果您使用 mx:Bindable 标记在一处添加反向绑定,则可能会更易读

      例如。

          <mx:Binding destination="company.companyName"   source="txtCname.text" />
          <mx:Binding destination="company.streetAddress" source="txtStreet.text" />
          ...
          etc.
      

      【讨论】:

        【解决方案3】:

        Flex 3 Cookbook 在第 11.9 章和第 14 章中有很多此类工作的食谱

        【讨论】:

          【解决方案4】:

          您需要将变量本身标记为可绑定:

          [Bindable]public var companyInfoId:int;
          

          欢迎使用 Flex 数据绑定,它很可爱 :)

          【讨论】:

          • -1。公共类定义之前的 [Bindable] 元数据标记使您定义为变量的所有公共属性以及使用 setter 和 getter 方法定义的所有公共属性都可用作绑定表达式的源。 livedocs.adobe.com/flex/3/html/…
          • 啊,没发现 :( 而且我还假设您可以双向绑定,就像您可以将 ItemRenders 直接绑定到带有 { } 的文本框一样。看起来您不能!
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-16
          相关资源
          最近更新 更多