數據綁定是將一個對象的數據綁定到另一個對象的過程。它需要一個源屬性、一個目標屬性和一個指示何時將數據從源複製到目標的觸發事件。
Flex提供了三種方法來進行數據綁定,如下所示
- Curly brace syntax in MXML Script ({})
- <fx:binding> tag in MXML
- BindingUtils in ActionScript
Data Binding – Using Curly Braces in MXML
下面的示例演示如何使用大括號指定源到目標的數據綁定。
<s:TextInput id = "txtInput1" /> <s:TextInput id = "txtInput2" text = "{txtInput1.text}" />
Data Binding – Using <fx:Binding> tag in MXML
下面的示例演示如何使用
<fx:Binding source = "txtInput1.text" destination = "txtInput2.text" /> <s:TextInput id = "txtInput1" /> <s:TextInput id = "txtInput2" />
Data Binding – Using BindingUtils in ActionScript
下面的示例演示如何使用BindingUtils指定源到目標的數據綁定。
<fx:Script> <![CDATA[ import mx.binding.utils.BindingUtils; import mx.events.FlexEvent; protected function txtInput2_preinitializeHandler(event:FlexEvent):void { BindingUtils.bindProperty(txtInput2,"text",txtInput1, "text"); } ]]> </fx:Script> <s:TextInput id = "txtInput1" /> <s:TextInput id = "txtInput2" preinitialize = "txtInput2_preinitializeHandler(event)" />
Flex Data Binding Example
讓我們按照下面給出的步驟,通過創建一個測試應用程式來查看Flex應用程式中的蒙皮操作;
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a packagecom.tutorialspoint.client as explained in the Flex - Create Application chapter. |
2 | Modify HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to make sure business logic is working as per the requirements. |
<?xml version = "1.0" encoding = "utf-8"?> <s:Application xmlns:fx = "http://ns.adobe.com/mxml/2009" xmlns:s = "library://ns.adobe.com/flex/spark" xmlns:mx = "library://ns.adobe.com/flex/mx width = "100%" height = "100%" minWidth = "500" minHeight = "500"> <fx:Style source = "/com/tutorialspoint/client/Style.css" /> <fx:Script> <![CDATA[ import mx.binding.utils.BindingUtils; import mx.events.FlexEvent; protected function txtInput6_preinitializeHandler(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" horizontalAlign = "center" verticalAlign = "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" preinitialize = "txtInput6_preinitializeHandler(event)" /> </s:HGroup> </s:Panel> </s:VGroup> </s:BorderContainer> </s:Application>
完成所有更改後,讓我們以正常模式編譯並運行應用程式,就像我們在flex-create application一章中所做的那樣。如果您的應用程式一切正常,它將產生以下結果:[聯機試用]