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:
<?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:
<?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:
<?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.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
18 Responses to Data binding in Flex
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


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…
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.
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.
Well done :)
A few examples of how to use the binding utilities with Objects and ArrayCollections would be most appreciated :).
Added SWFs.
Peter
Hi,
Is it possible to bind to a function that requires a parameter ?
ex :
[Bindable("change")]
public function getString(obj:Object):String
{
…
}
Thanks
thanks for the code, but the text in the destination loses its property like font size, font type, font color on pressing a space bar, how to avoid that
A few examples of how to use the binding utilities with Objects and ArrayCollections would be most appreciated too.
Another way could be to use Actionscript only as described in a blogpost of mine. It shows you how you can bind to a component and use a labelFunction to return a value, instead of the actual value (e.g.: a formatted datestring instead of a date object).
http://webdevotion.be/blog/2009/04/20/binding-between-a-datefield-and-a-button-label-at-runtime/
very nice example, thank you very much!
This post not render on Chrome.
Works for me on Vista 64 w/ Google Chrome 2.0.172.33.
Peter,
Thanks for the site – I refer to it frequently. On to the matter at hand:
I am attempting to bind an object’s property to another object’s property for writing to a mySQL database. It works correctly, however, I need to do this task a dynamic number of times based on user interaction with the datagrid (based on how many items they have selected in the “multipleSelectionsAllowed” datagrid).
code is something like:
for each (var obj:Object in myObjCollection) //myObjCollection is the list of selected items in datagrid
{
BindingUtils.bindProperty(_destObj, "myProp", obj, "myProp");
var asyncToken:AsyncToken = _destObj.save();
}
where destObj is defined as the same type object as obj
The line “var asyncToken:AsyncToken = _destObj.save();” works fine outside of the loop with a separate binding of source/dest in but only does this for one item, not each in the list.
Any help is much appreciated :D
By the way, the error I am getting is something like “Record is locked and can’t be saved” – this only occurs in the for loop, not in the single call (which works/updates correctly)
Since bi-directional binding is not supported in Flex 3, you can use a mapping file and a binder class using the mapping file to support bi-directional binding.
Furthermore this will reduce code and extract the mapping/binding concern from your main application code.
Sample: Flex binding
Kind regards,
Marc
Could these binding utils be used to bind the text of a RTE (including the size/weight/color/etc…) to a label/text area? I’ve set the function up to bind between a RTE and a label, but it won’t carry over the weight/size/color of the text. It will on the other hand carry over the font type. Am I missing something? I have a feeling it’s something to do with the fontarrays and the like with the RTE, but I was wondering if I could get some input on this.
Thanks.
Very nice example and clear explanation.Thanks for sharing.