不了解动态数据绑定(bindPropety)的基础知识在Flex中

Jos*_*hua 5 apache-flex air actionscript actionscript-3

我需要动态绑定在运行时创建的组件的属性.在这种特殊情况下,请假设我需要使用bindProperty.

我不太明白为什么下面的简单测试失败了(参见代码).单击按钮时,标签文本不会更改.

我意识到使用传统的非动态绑定有更简单的方法可以解决这个特定的例子,但我需要在使用bindProperty方面理解它.

有人可以帮我理解我错过的东西吗?

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="Tools.*" minWidth="684" minHeight="484" xmlns:ns2="*" creationComplete="Init();">
  <mx:Script>
    <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.binding.utils.*;
      public var Available:ArrayCollection=new ArrayCollection();

      public function get Value():String {
        return (Available.getItemAt(0).toString());
      }

      public function Init():void {
        Available.addItemAt('Before', 0);
        BindingUtils.bindProperty(Lab, 'text', this, 'Value');
      }

      public function Test():void {
        Available.setItemAt('After', 0);
      }
    ]]>
  </mx:Script>
  <mx:Label x="142" y="51" id="Lab"/>
  <mx:Button x="142" y="157" label="Button" click="Test();"/>
</mx:WindowedApplication>
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Ama*_*osh 3

正如 Glenn 所提到的,您需要[Bindable]在 上添加标签Value

此外,您还没有为该属性定义 setter。仅当调用相应的 setter 时,才会调用数据绑定。流程类似于:您调用 setter - Flex 通过调用 getter 来更新数据。

  [Bindable]
  public function get value():String {
    return (Available.getItemAt(0).toString());
  }

  public function set value(v:String):void {
    Available.setItemAt(v, 0);
  }

  public function init():void {
    Available.addItemAt('Before', 0);
    BindingUtils.bindProperty(Lab, 'text', this, 'Value');
  }

  public function iest():void {
    value = "After";
  }
Run Code Online (Sandbox Code Playgroud)

请注意,我已按照正常约定将函数和属性的名称更改为小写。InitialCaps 仅用于类名。