Skip to main content

Posts

Showing posts from January, 2009

Manually adding bindings between objects in Flex/AS3

I recently needed to manually create bindings in AS3 code between objects, and since MXML ultimately all is just AS3, I figured there had to be a way to do it.

<b>Binding in MXML</b>

Lets look at the following code...
<code>
    <mx:TextInput id="txtMyText?/>
    <mx:Label id="lblMyLabel? text="{txtMyText.text}"/>
</code>

Here you bind the text property of the TextInput to the label's text property so wh you type in the textInput, it reflects in the label.

<b>Binding in AS</b>

The key to this is a class called BindingUtils in the mx.binding.utils package. You can use this class as below:<more/>You have your components say in mxml as follows
<code>
    <mx:TextInput id="txtMyText? />
    <mx:Label id="lblMyLabel? />
</code>

Now you import the Binding utils and change watcher classes:
<code>
    import mx.binding.utils.BindingUtils;
    import mx.binding.utils.ChangeWat…