用于绑定的动态变量

时间:2014-12-22 21:38:32

标签: actionscript-3 flex binding

我有一个变量myVar,它可以带几个值(1,2,3等......)。 我还有很多s:Group名为G1G2G3等...

如何在{}中定义动态变量名以进行绑定?

类似的东西:

<s:Group id="anotherGroup" height="{Group(this['G' + myVar]).height}" />

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

你所做的对我来说很好看。下面是使用变量作为组件ID来访问属性并将其用于绑定的示例:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
  <fx:Script><![CDATA[
    [Bindable]
    private var myVar:int = 1;

    private function changeGroup_clickHandler(event:MouseEvent):void
    {
        if(++myVar > 3)
        {
            myVar = 1;
        }        
    }
    ]]></fx:Script>
  <s:VGroup height="500" width="1000">
    <s:Group id="G1" height="25" width="100%" contentBackgroundColor="red">
        <s:BorderContainer borderColor="red" height="100%" width="100%">
            <s:Label text="Inside G1. Height: {G1.height}"/>
        </s:BorderContainer>
    </s:Group>
    <s:Group id="G2" height="50" width="100%" >
        <s:BorderContainer borderColor="blue" height="100%" width="100%">
            <s:Label text="Inside G2. Height: {G2.height}"/>
        </s:BorderContainer>
    </s:Group>
    <s:Group id="G3" height="75" width="100%">
        <s:BorderContainer borderColor="green" height="100%" width="100%">
            <s:Label text="Inside G3. Height: {G3.height}"/>
        </s:BorderContainer>
    </s:Group>
    <s:Group id="anotherGroup" height="{(this['G'+myVar] as Group).height}" width="100%">
        <s:BorderContainer borderColor="yellow" height="100%" width="100%">
            <s:Label text="Inside anotherGroup. Height: {anotherGroup.height}"/>
        </s:BorderContainer>
    </s:Group>
    <s:Button label="Change Group" click="changeGroup_clickHandler(event)"/>
  </s:VGroup>
</s:Application>