Flex ItemRender不更新数据源

时间:2014-10-28 14:08:28

标签: actionscript-3 flex itemrenderer 2-way-object-databinding

首先很抱歉大量的代码和糟糕的编码,自从我完成AS3 / Flex以来已有一段时间了,在我的生活中,我不记得如何在ItemRender中获取ItemRenderer来更新数据源。例如, GymButton2.mxml simple在单击按钮时为其提供的数据添加+1,这应该更新原始数据源activeGym但是......它没有,我认为这是因为绑定没有按预期工作?

Main.mxml          

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;

            [Bindable]
            public var activeGym:Gym2;

        ]]>
    </fx:Script>

    <s:DataGroup x="33" y="56" width="200" height="200" dataProvider="{activeGym.sets}" itemRenderer="GymSetRenderer">
        <s:layout>
            <s:VerticalLayout />
        </s:layout>
    </s:DataGroup>

</s:WindowedApplication>

Gym2.as

package
{
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;

    import mx.collections.ArrayCollection;

    [Bindable]

    public class Gym2 extends EventDispatcher
    {

        public var sets:ArrayCollection = new ArrayCollection();

        public function Gym2(target:IEventDispatcher=null)
        {
            super(target);
        }

        public function addSet(set:GymSet):void {
            sets.addItem(set);
        }

    }
}

GymSetRenderer.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" 
                autoDrawBackground="false" xmlns:local="*">

    <s:layout>
        <s:VerticalLayout />
    </s:layout>

    <s:Label fontSize="27" text="{data.title}"/>

    <s:DataGroup dataProvider="{data.data}" itemRenderer="GymButton2">
        <s:layout>
            <s:HorizontalLayout />
        </s:layout>
    </s:DataGroup>

</s:ItemRenderer>

GymButton2.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" autoDrawBackground="true">

    <fx:Script>
        <![CDATA[
            protected function onClick(event:MouseEvent):void
            {
                data = data + 1;
            }
        ]]>
    </fx:Script>

    <s:Button label="{data}" height="70" click="onClick(event)"/>

</s:ItemRenderer>

0 个答案:

没有答案