- Flex - Printing Support
- Flex - Internationalization
- Flex - Debug Application
- Flex - FlexUnit Integration
- Flex - RPC Services
- Flex - Custom Controls
- Flex - Event Handling
- Flex - Visual Effects
- Flex - Layout Panels
- Flex - Complex Controls
- Flex - Form Controls
- Flex - Basic Controls
- Flex - Data Binding
- Flex - Style with Skin
- Flex - Style with CSS
- Flex - Life Cycle Phases
- Flex - Deploy Application
- Flex - Create Application
- Flex - Applications
- Flex - Environment
- Flex - Overview
- Flex - Home
Adobe Flex Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Flex - Data Binding
What is Data Binding?
Data Binding is a process in which data of one object is tied to another object. It requires a source property, a destination property and a triggering event which indicates, when to copy the data from source to destination.
Flex provides three ways to do Data Binding as below
Curly brace syntax in MXML Script ({})
<fx:binding> tag in MXML
BindingUtils in ActionScript
Data Binding – Using Curly Braces in MXML
The following example demonstrates how to use curly braces to specify data binding of a source to destination.
<s:TextInput id = "txtInput1" /> <s:TextInput id = "txtInput2" text = "{txtInput1.text}" />
Data Binding – Using <fx:Binding> tag in MXML
The following example demonstrates how to use
<fx:Binding source = "txtInput1.text" destination = "txtInput2.text" /> <s:TextInput id = "txtInput1" /> <s:TextInput id = "txtInput2" />
Data Binding – Using BindingUtils in ActionScript
The following example demonstrates how to use BindingUtils to specify data binding of a source to destination.
<fx:Script> <![CDATA[ import mx.binding.utils.BindingUtils; import mx.events.FlexEvent; protected function txtInput2_preinitiapzeHandler(event:FlexEvent):void { BindingUtils.bindProperty(txtInput2,"text",txtInput1, "text"); } ]]> </fx:Script> <s:TextInput id = "txtInput1" /> <s:TextInput id = "txtInput2" preinitiapze = "txtInput2_preinitiapzeHandler(event)" />
Flex Data Binding Example
Let us follow the steps given below to see skinning in action in a Flex apppcation by creating a test apppcation −
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a packagecom.tutorialspoint.cpent as explained in the Flex - Create Apppcation chapter. |
2 | Modify HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Compile and run the apppcation to make sure business logic is working as per the requirements. |
Following is the content of the modified HelloWorld.mxml filesrc/com/tutorialspoint/cpent/HelloWorld.mxml.
<?xml version = "1.0" encoding = "utf-8"?> <s:Apppcation xmlns:fx = "http://ns.adobe.com/mxml/2009" xmlns:s = "pbrary://ns.adobe.com/flex/spark" xmlns:mx = "pbrary://ns.adobe.com/flex/mx width = "100%" height = "100%" minWidth = "500" minHeight = "500"> <fx:Style source = "/com/tutorialspoint/cpent/Style.css" /> <fx:Script> <![CDATA[ import mx.binding.utils.BindingUtils; import mx.events.FlexEvent; protected function txtInput6_preinitiapzeHandler(event:FlexEvent):void { BindingUtils.bindProperty(txtInput6,"text",txtInput5, "text"); } ]]> </fx:Script> <fx:Binding source = "txtInput3.text" destination = "txtInput4.text" /> <s:BorderContainer width = "500" height = "550" id = "mainContainer" styleName = "container"> <s:VGroup width = "100%" height = "100%" gap = "50" horizontalApgn = "center" verticalApgn = "middle"> <s:Label id = "lblHeader" text = "Data Binding Demonstration" fontSize = "40" color = "0x777777" styleName = "heading" /> <s:Panel title = "Example #1 (Using Curly Braces,{})" width = "400" height = "100" > <s:layout> <s:VerticalLayout paddingTop = "10" paddingLeft = "10" /> </s:layout> <s:HGroup > <s:Label text = "Type here: " width = "100" paddingTop = "6" /> <s:TextInput id = "txtInput1" /> </s:HGroup> <s:HGroup > <s:Label text = "Copied text: " width = "100" paddingTop = "6" /> <s:TextInput id = "txtInput2" text = "{txtInput1.text}" /> </s:HGroup> </s:Panel> <s:Panel title = "Example #2 (Using <fx:Binding>)" width = "400" height = "100" > <s:layout> <s:VerticalLayout paddingTop = "10" paddingLeft = "10" /> </s:layout> <s:HGroup > <s:Label text = "Type here: " width = "100" paddingTop = "6" /> <s:TextInput id = "txtInput3" /> </s:HGroup> <s:HGroup > <s:Label text = "Copied text: " width = "100" paddingTop = "6" /> <s:Label id = "txtInput4" /> </s:HGroup> </s:Panel> <s:Panel title = "Example #3 (Using BindingUtils)" width = "400" height = "100" > <s:layout> <s:VerticalLayout paddingTop = "10" paddingLeft = "10" /> </s:layout> <s:HGroup > <s:Label text = "Type here: " width = "100" paddingTop = "6" /> <s:TextInput id = "txtInput5" /> </s:HGroup> <s:HGroup > <s:Label text = "Copied text: " width = "100" paddingTop = "6" /> <s:TextInput enabled = "false" id = "txtInput6" preinitiapze = "txtInput6_preinitiapzeHandler(event)" /> </s:HGroup> </s:Panel> </s:VGroup> </s:BorderContainer> </s:Apppcation>
Once you are ready with all the changes done, let us compile and run the apppcation in normal mode as we did in
chapter. If everything is fine with your apppcation, it will produce the following result: [ ]![Flex Data Binding](/flex/images/flex_data_binding.jpg)