组合框的 selectedItem 的两种方式数据绑定无法通过 Adob​​e Flex 4 中的 @ 符号工作

发布于 2024-11-26 22:26:49 字数 3801 浏览 1 评论 0原文

我需要在组合框的 selectedItem 和 valueobject 的字段之一中启用两种方式的数据绑定。我正在使用 @{变量名} 构造。

它以一种方式工作 - 当 valueobject 的字段更改时,组合框的 selectedItem 也会更新。 但除非我显式处理组合框的更改事件,否则反向不起作用。 @ 是否有任何原因无法按预期工作。

在下面的代码片段中,我尝试将 OrderInfo.billingName 绑定到combo1.selectedItem。

第一个用例:OrderInfo.billingName的初始值设置为combo1.selectedItem

第二个用例:如果OrderInfo.billingName值在两者之间发生更改,则combo1.selectedItem也会发生变化正在更新

第三个用例:当用户从combo1中选择某个值时,它不会被分配给OrderInfo.billingName,除非我处理更改事件。

[可绑定]

公共类 OrderInfo {

        public var billingName:String ; //This field is bindable to combo1’s selectedItem
        public var billingAddress:String;
        public var billingCity:String;
        public var billingState:String;
        public var billingZip:String;
        public var cardType:String;
        public var cardNumber:String;
        public var cardExpirationMonth:String;
        public var cardExpirationYear:String;
        public var deliveryDate:Date;

        public function OrderInfo() {
        }
        public function toString ():String {
              return "I am OrderInfo : " + this.billingName + this.billingCity;
        }
  }

<?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"
                 initialize="application1_initializeHandler(event)">
  <fx:Script>
        <![CDATA[
              import mx.binding.utils.BindingUtils;
              import mx.collections.ArrayCollection;
              import mx.controls.Alert;
              import mx.events.FlexEvent;
              import mx.events.IndexChangedEvent;
              import mx.utils.ObjectUtil;

              import spark.events.IndexChangeEvent;
              [Bindable]
              public var dp:ArrayCollection = new ArrayCollection (
                    [     {Id:'one', Amount:1000},
                          {Id:'two', Amount:2000},
                          {Id:'three', Amount:3000}
                    ]
              );
              [Bindable]
              public var orderInfo:OrderInfo = new OrderInfo();
              protected function application1_initializeHandler(event:FlexEvent):void
              {
                    //Initial value of the field .. this could be coming via database
                    orderInfo.billingName = 'one';
              }


              protected function combo1_changeHandler(event:IndexChangeEvent):void
              {
                    orderInfo.billingName = (((event.currentTarget as 
                    ComboBox).selectedItem as Object).Id); //??
              }

              protected function button1_clickHandler(event:Event):void
              {
                    mx.controls.Alert.show(ObjectUtil.toString(orderInfo));
              }

              protected function button2_clickHandler(event:Event):void
              {
                    // Some backend process changed the value object
                    orderInfo.billingName = 'three';
              }
        ]]>
  </fx:Script>

  <s:ComboBox id="combo1" x="81" y="65"
                    dataProvider="{dp}"
                    labelField="Id"
                    selectedItem="@{orderInfo.billingName}"
                    change="combo1_changeHandler(event)"
                    />

  <s:Button label="Get OrderInfo Object Snapshot" click="button1_clickHandler(event)" 
   x="273" y="176"/>
  <s:Button label="Change OrderInfo Object" click="button2_clickHandler(event)" 
   x="52"    y="176"/>

I need to enable two way data binding in combobox’s selectedItem and a valueobject’s one of the field. I am using @{variable name} construct.

It works one way - when valueobject’s field is changed, combobox’s selectedItem is getting updated.
But reverse is not working unless I handle combobox’s change event explicitly.
Is there any reason for @ not working as per expectation.

In below code snippet, I am trying to bind OrderInfo.billingName to combo1.selectedItem.

1st use case : OrderInfo.billingName’s initial value is getting set to combo1.selectedItem

2nd use case: In case OrderInfo.billingName value is changed in between, then also combo1.selectedItem is getting updated

3rd use case : When user select some value from combo1, it is not getting assigned to OrderInfo.billingName unless I handle change event.

[Bindable]

public class OrderInfo {

        public var billingName:String ; //This field is bindable to combo1’s selectedItem
        public var billingAddress:String;
        public var billingCity:String;
        public var billingState:String;
        public var billingZip:String;
        public var cardType:String;
        public var cardNumber:String;
        public var cardExpirationMonth:String;
        public var cardExpirationYear:String;
        public var deliveryDate:Date;

        public function OrderInfo() {
        }
        public function toString ():String {
              return "I am OrderInfo : " + this.billingName + this.billingCity;
        }
  }

<?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"
                 initialize="application1_initializeHandler(event)">
  <fx:Script>
        <![CDATA[
              import mx.binding.utils.BindingUtils;
              import mx.collections.ArrayCollection;
              import mx.controls.Alert;
              import mx.events.FlexEvent;
              import mx.events.IndexChangedEvent;
              import mx.utils.ObjectUtil;

              import spark.events.IndexChangeEvent;
              [Bindable]
              public var dp:ArrayCollection = new ArrayCollection (
                    [     {Id:'one', Amount:1000},
                          {Id:'two', Amount:2000},
                          {Id:'three', Amount:3000}
                    ]
              );
              [Bindable]
              public var orderInfo:OrderInfo = new OrderInfo();
              protected function application1_initializeHandler(event:FlexEvent):void
              {
                    //Initial value of the field .. this could be coming via database
                    orderInfo.billingName = 'one';
              }


              protected function combo1_changeHandler(event:IndexChangeEvent):void
              {
                    orderInfo.billingName = (((event.currentTarget as 
                    ComboBox).selectedItem as Object).Id); //??
              }

              protected function button1_clickHandler(event:Event):void
              {
                    mx.controls.Alert.show(ObjectUtil.toString(orderInfo));
              }

              protected function button2_clickHandler(event:Event):void
              {
                    // Some backend process changed the value object
                    orderInfo.billingName = 'three';
              }
        ]]>
  </fx:Script>

  <s:ComboBox id="combo1" x="81" y="65"
                    dataProvider="{dp}"
                    labelField="Id"
                    selectedItem="@{orderInfo.billingName}"
                    change="combo1_changeHandler(event)"
                    />

  <s:Button label="Get OrderInfo Object Snapshot" click="button1_clickHandler(event)" 
   x="273" y="176"/>
  <s:Button label="Change OrderInfo Object" click="button2_clickHandler(event)" 
   x="52"    y="176"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情哥ㄟ 2024-12-03 22:26:49

我也在考虑让这个功能发挥作用。我认为问题在于所约束的内容。 selectedItem 属性需要一个 * 类型的对象,这意味着它所绑定的 arrayCollection 中的一个项目。因此,请尝试将一个或这些对象

({Id:'one', Amount:1000},{Id:'two', Amount:2000},{Id:'three', Amount:3000})

而不是字符串传递给 selectedItem 属性。

I am also looking into getting this functionality working. I believe the issue lies in what is being bound. The selectedItem property expects an object of type * which means an item from the arrayCollection that it's bound with. So try passing one or these objects

({Id:'one', Amount:1000},{Id:'two', Amount:2000},{Id:'three', Amount:3000})

to the selectedItem property instead of a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文