Flex 3绑定问题

时间:2009-12-11 10:13:26

标签: flex data-binding binding bindable

我有一个自定义的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;


}
}

我想将这些值绑定到textBoxes,这样当用户向textBoxes输入信息时,它也会在类中填充。 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新手,任何帮助都会得到满足......谢谢...

4 个答案:

答案 0 :(得分:3)

不,Flex数据绑定不是双向的。您必须使用BindingUtils.bindProperty()

将文本字段的text属性显式绑定回company对象的相应属性
private function init():void 
{
    company = new CompanyInfo();
    BindingUtils.bindProperty(company, "companyName", this ["txtCname", "text"]);
}

答案 1 :(得分:3)

如果您在MXML组件中使用它,如果您使用mx:Bindable标记在一个地方添加反向绑定

,它可能更具可读性

例如

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

答案 2 :(得分:0)

Flex 3 Cookbook 在第11.9章和第11章中为此类工作提供了大量的方法。第14页

答案 3 :(得分:-1)

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

[Bindable]public var companyInfoId:int;

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