Adobe Flex 3 ヘルプ

データバインディング式での E4X 式の使用

中括弧または <mx:Binding> タグ内のバインディング式には、バインディングのソースが XML 型のバインド可能プロパティである場合に、ECMAScript for XML(E4X)式を含めることができます。BindingUtils.bindProperty() または BindingUtils.bindSetter() メソッドを使用してデータバインディングを定義する場合、E4X を使用することはできません。

中括弧内での E4X 式の使用

バインディング先が String プロパティの場合は、中括弧のバインディング式から toString() メソッドが自動的に呼び出されます。中括弧または <mx:Binding> タグ内のバインディング式には、バインディングのソースが XML 型のバインド可能プロパティである場合に、ECMAScript for XML(E4X)式を含めることができます。詳しくは、<mx:Binding> タグ内での E4X 式の使用を参照してください。

次の例のコードには、XML オブジェクトからのデータをバインドする 3 つのバインディング式が中括弧内にあります。1 つ目では .(1 つのドット表記)を、2 つ目では ..(2 つのドット表記)を、3 つ目では ||(OR 表記)を、それぞれ使用しています。

<?xml version="1.0"?>
<!-- binding/E4XInBraces.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[
        
            [Bindable]
            public var xdata:XML = <order>
                <item id = "3456">
                    <description>Big Screen Television</description>
                    <price>1299.99</price><quantity>1</quantity>
                </item>
                <item id = "56789">
                    <description>DVD Player</description>
                    <price>399.99</price>
                    <quantity>1</quantity>
                </item>
            </order>;
        ]]>
    </mx:Script>

    <mx:Label text="Using .. notation."/>
    <!-- Inline databinding will automatically call the 
        toString() method when the binding destination is a string. -->
    <mx:List width="25%" 
        dataProvider="{xdata..description}"/>
    
    <mx:Label text="Using . notation."/>
    <mx:List width="25%" 
        dataProvider="{xdata.item.description}"/>

    <mx:Label text="Using || (or) notation."/>
    <mx:List width="25%" 
        dataProvider="{xdata.item.(@id=='3456'||@id=='56789').description}"/>
</mx:Application>

前の例で実行する SWF ファイルは以下のとおりです。

<mx:Binding> タグ内での E4X 式の使用

中括弧内の E4X 式とは異なり、<mx:Binding> タグ内で E4X 式を使用する際は、バインディング先が String プロパティの場合に toString() メソッドを明示的に呼び出す必要があります。

次の例のコードには、XML オブジェクトからのデータをバインドする 3 つのバインディング式が中括弧内にあります。1 つ目では .(1 つのドット表記)を、2 つ目では ..(2 つのドット表記)を、3 つ目では ||(OR 表記)を、それぞれ使用しています。

<?xml version="1.0"?>
<!-- binding/E4XInBindingTag.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    width="600" height="900">

    <mx:Script>
        <![CDATA[
            [Bindable]
            public var xdata:XML = 
                <order>
                    <item id = "3456">
                        <description>Big Screen Television</description>
                        <price>1299.99</price><quantity>1</quantity>
                    </item>
                    <item id = "56789">
                        <description>DVD Player</description>
                        <price>399.99</price>
                        <quantity>1</quantity>
                    </item>
                </order>;        
        ]]>
    </mx:Script>

    <mx:Label text="Using .. notation."/>

    <!-- This will update because what is 
        binded is actually the String and XMLList. -->
    <mx:List width="75%" id="txts"/>
    <mx:Binding 
        source="xdata..description" 
        destination="txts.dataProvider"/>

    <mx:Label text="Using . notation."/>
    <mx:List width="75%" id="txt2s"/>
    <mx:Binding 
        source="xdata.item.description" 
        destination="txt2s.dataProvider"/>

    <mx:Label text="Using || (or) notation."/>
    <mx:List width="75%" id="txt3s"/>
    <mx:Binding 
        source="xdata.item.(@id=='3456'||@id=='56789').description" 
        destination="txt3s.dataProvider"/>
</mx:Application>

前の例で実行する SWF ファイルは以下のとおりです。

 

このページに新しいコメントが追加された場合に、電子メールでの通知を希望する。 | コメントレポート