James Lei
James Lei

Reputation: 19

Binding update on label

I would like to bind a variable which if there a changes, it will automatically update the label.

var disp:String = "00:00:00";
var lb:Label = new Label();  //Add Label to an "ContentGroup" container.
lb.text = totalTime;
addElement(lb);

disp="00:00:01"; //New timing

BindingUtils.bindProperty(totalTime, "text", disp, "text");

How do I do that?

Upvotes: 0

Views: 131

Answers (1)

Harry Ninh
Harry Ninh

Reputation: 16718

Firstly, I want to clarify your question. You want to bind Label lb with disp, then later whenever disp change, lb.text change, is it?

  1. Why don't use MXML (which perfectly matches with Binding mechanism) instead?

  2. If you really want to use Actionscript, there are some points need your attention. Firstly, the "host" property has to be bindable (add [Bindable] tag on top). Next, BindingUtils.bindProperty should directly bind two values with each other. In this case, change to following code will work:

    [Bindable] public var disp: String = "00:00:00";
    
    private function initBinding(): void {
    
    var lb: Label = new Label();
    
    addElement( lb );
    
    BindingUtils.bindProperty( lb, "text", this, "disp" );
    
    }
    

By the way, I'm always avoiding using Binding if possible. Manually update properties in simple cases will save both file size and performance.

Upvotes: 1

Related Questions