2 Replies Latest reply on Feb 12, 2009 12:22 AM by Txoov

    Dynamic control binding

    Txoov Level 1
      I'm trying to bind a new Label control to a variable String like below.

      var str:String = "Hello World";
      var lbl:Label = new Label();
      lbl.text = "{str}" // I know this doesn't work

      I would prefer to set the lbl.text to the string like below but that doesn't work either. I get a undefined property error since the control doesn't exist during compile time.

      lbl.text = "Hello World";

      Any help would be appreciated. Thanks...
        • 1. Re: Dynamic control binding
          Cesare Rocchi Level 2
          Crea simple Flex app like this:

          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          [Bindable] private var test:String = "test";

          <mx:Label text="{test}"/>

          Run the compiler with the "-keep-generated-actionscript" argument.
          Check out the generated code and you will be close to the solution.

          Hint: exploit the PropertyChangeEvent.

          • 2. Re: Dynamic control binding
            Txoov Level 1
            Thanks for the tip, funkyboy. I didn't know you could set the compiler to -keep. Wow, so few mxml generates tons of .as code. I chose not to go with the PropertyChangeEvent caused that seemed like more work than needed. So what I did was create a private label, then set that label to my dynamic label, and then set the private label.text to whatever I wanted.

            private var lblPrivate:Label;
            public function createDynamicLabel():void
            var myLabel:Label = new Label();
            lblPrivate = myLabel;
            public function setLabelText():void
            lblPrivate.text = "Hello World";