01
Oct
07

Data binding in Flex

The following examples show a few different ways to bind data in Flex.

Full code after the jump.

The following example shows how you can use the <mx:Binding /> tag to bind values between two controls:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/01/data-binding-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Binding source="textInputSrc.text"
            destination="textInputDst.text" />

    <mx:Form>
        <mx:FormItem label="source:">
            <mx:TextInput id="textInputSrc" />
        </mx:FormItem>
        <mx:FormItem label="destination:">
            <mx:TextInput id="textInputDst"
                    width="{textInputSrc.width}" />
        </mx:FormItem>
    </mx:Form>

</mx:Application>

View source is enabled in the following example.

The following example shows how you can use the static BindingUtils.bindProperty() method to bind values between two controls:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/01/data-binding-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.binding.utils.BindingUtils;

            private function init():void {
                BindingUtils.bindProperty(textInputDst, "text", textInputSrc, "text");
            }
        ]]>
    </mx:Script>

    <mx:Form>
        <mx:FormItem label="source:">
            <mx:TextInput id="textInputSrc" />
        </mx:FormItem>
        <mx:FormItem label="destination:">
            <mx:TextInput id="textInputDst"
                    width="{textInputSrc.width}" />
        </mx:FormItem>
    </mx:Form>

</mx:Application>

View source is enabled in the following example.

The following example shows how you can use the static BindingUtils.bindSetter() method to call a method whenever the source property is changed:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/01/data-binding-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.binding.utils.BindingUtils;

            private function init():void {
                BindingUtils.bindSetter(setterFunc, textInputSrc, "text");
            }

            private function setterFunc(str:String):void {
                textInputDst.text = str;
            }
        ]]>
    </mx:Script>

    <mx:Form>
        <mx:FormItem label="source:">
            <mx:TextInput id="textInputSrc" />
        </mx:FormItem>
        <mx:FormItem label="destination:">
            <mx:TextInput id="textInputDst"
                    width="{textInputSrc.width}" />
        </mx:FormItem>
    </mx:Form>

</mx:Application>

View source is enabled in the following example.


7 Responses to “Data binding in Flex”


  1. 1 Clint Oct 10th, 2007 at 11:19 am

    Hmm, nice. But why would I use anything but [Bindable]? Can you include reasons, explainations, or circumstances where one might want to use these alternatives instead? Pros and cons or something…

  2. 2 Ryan Nov 6th, 2007 at 3:46 pm

    One scenario where you might use the BindingUtils is when you generate UI components at runtime based on the results of a dataservice. This can be handy for performing binding on non-data aware display components such as Grid, where the the state maintenance is largely up to the developer and what child components the grid contains.

  3. 3 Simon Feb 13th, 2008 at 7:28 am

    I had the situation when using the Cairngorm framework that I needed to update a mx:progressbar component using the setProgress function whenever a variable in the ModelLocator changed. I used the BindingUtils.bindSetter to call the function whenever the variable changed, just to give you another example.

  4. 4 guillaume.jt Apr 1st, 2008 at 2:27 am

    Well done :)

  5. 5 Derek Basch Apr 24th, 2008 at 4:19 pm

    A few examples of how to use the binding utilities with Objects and ArrayCollections would be most appreciated :).

  6. 6 peterd May 31st, 2008 at 12:53 pm

    Added SWFs.

    Peter

  7. 7 Alex Jul 25th, 2008 at 12:15 pm

    Hi,

    Is it possible to bind to a function that requires a parameter ?

    ex :

    [Bindable(”change”)]
    public function getString(obj:Object):String
    {

    }

    Thanks

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".