---
title: "[Flex]Flex绑定数据的方式"
author: "heyJoe"
date: "2008-08-21T09:58:09Z"
tags: ["ActionScript", "Flex"]
slug: "flex-flex"
draft: false
lastmod: "2008-08-21T09:58:09Z"
id: 230923
url: "https://heyjoe.iteye.com/blog/230923"
---

在使用Flex开发的过程中，数据绑定是一定会遇到的，这种技术简单，又有点好玩，重要的是它让开发变得简单了。

 

在Flex中，数据绑定的方式有这么三种：

- 直接在“{}”中填写绑定变量
- 使用&lt;mx:Binding /&gt;标签绑定
- 使用ActionScript中的BindingUtils类绑定

    示例1:

```
<mx:TextInput id="inputUnit" />
<mx:Label text="{inputUnit.text}" />
```

 

示例1中的绑定方式即是第一种绑定方式，在“{}”中直接填写inputUnit.text，表示Label显示的内容和TextInput是关联的，它会随着TextInput的输入内容变化而变化。

 

    示例2:

```
<mx:TextInput id="inputUnit" />
<mx:Label id="showUnit" />
<mx:Binding source="inputUnit.text" destination="showUnit.text" />
```

 

示例2展示了第二种绑定方式，使用&lt;mx:Binding /&gt;标签。

 

    示例3:

```
	<mx:Script>
		<![CDATA[
			
			import mx.binding.utils.*;
						
			private function initBind():void {
		
				BindingUtils.bindProperty(showUnit, "text", inputUnit, "text");		
			}
			
		]]>
	</mx:Script>
	
	
	<mx:TextInput id="inputUnit" />
	<mx:Label id="showUnit" />

```

 

 

示例3使用第三种方式来绑定数据，在我们通过调用initBind()方法之后，绑定目的就达到了。

 

 

待续...