Flex3學習筆記5

來源:互聯網
上載者:User

Flex3學習筆記5

Flex3資料繫結

資料繫結是Flex非常棒的特徵之一,它給了輕易傳遞資訊的能力。


資料繫結:當資料來源對象的資料發生變化時,目標對象的資料會自動更新,而不需要再編寫代碼去強制更新。


綁定實際也是藉助事件機制來完成的,當目標使用了資料繫結的時候,目標對象就會監聽資料來源對象的某一固定事件。當資料來源發生變化時,資料來源會派發改變事件(ChangeEvent),通知目標對象更新資料。這個過程由Flex完成,不需手動幹預。


綁定的前提條件:來源物件的資料和目標對象的資料格式相同。

1、在對象的屬性標籤中,使用{ }把資料來源直接綁定到對象的某個屬性上。
如:

  1. <mx:TextInput id="helloTextInput" text="Hello, World" />
  2. <mx:Label text="{helloTextInput.text}" />

你還可以建立兩個字串變數,並綁定其中一個的值到Label標籤上:

  1. <mx:String id="firstName">Alaric</mx:String>
  2. <mx:String id="lastName">Cole</mx:String>
  3. <mx:Label id="nameLabel" text="{firstName}" />

綁定到多個目的標籤的情況:

  1. <mx:String id="displayName">Jed90210</mx:String>
  2. <mx:Label id="nameLabel" text="{displayName}" />
  3. <mx:Button id="nameButton" label="{displayName}" />

還可以進行字串的串聯,如:

  1. <mx:String id="displayName">Jed90210</mx:String>
  2. <mx:Label text="{'Hello, '+displayName}" />

更為複雜的字串串聯情況:

  1. <mx:String id="firstName">Alaric</mx:String>
  2. <mx:String id="lastName">Cole</mx:String>
  3. <mx:Label text="{'Hello, '+firstName+' '+lastName}" />

還有一種等價的方法,如下:

  1. <mx:Label text="Hello, {firstName} {lastName}" />

使用花括弧進行計算,如下:

  1. <mx:Label text="Eleven times forty-two equals {11*42}" />

或者是:

  1. <mx:Label text="Hey {firstName}, eleven times forty-two equals {11*42}" />

2、在對象的屬性標籤中,使用{ }把某個函數的傳回值作為資料來源綁定到對象屬性上。
函數的參數要使用[Bindable]綁定符號
[Bindable]
[Bindable(event="eventname")]
Event表示當資料來源發生變化時,資料來源所在對象派發的事件類型,它是可選項,預設的事件名是“propertyChange”,一般情況下只需要使用[Bindable]標籤。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.