Actionscript 3 在flex4中,如何在运行时向datagrid添加项?

Actionscript 3 在flex4中,如何在运行时向datagrid添加项?,actionscript-3,apache-flex,flex4,Actionscript 3,Apache Flex,Flex4,我的UserInfo表单包含两个输入字段username、location(city)和一个单选按钮作为性别,以及两个按钮add和reset。当我单击add按钮时,数据将在运行时添加到datagrid中。 我无法重现我将如何做到这一点。 有人能帮我举个例子吗?这是一个示例应用程序(带代码): 有几件事留给你做练习:) <?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://

我的UserInfo表单包含两个输入字段username、location(city)和一个单选按钮作为性别,以及两个按钮add和reset。当我单击add按钮时,数据将在运行时添加到datagrid中。 我无法重现我将如何做到这一点。 有人能帮我举个例子吗?

这是一个示例应用程序(带代码):


有几件事留给你做练习:)

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

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

            private var ac:ArrayCollection = new ArrayCollection();

            protected function addBtn_clickHandler(event:MouseEvent):void
            {
                var obj:Object = new Object();
                obj.userName = userNameTI.text;
                obj.location = locationTI.text;
                ac.addItem(obj);
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <mx:Form width="100%">
        <mx:FormItem label="UserName:">
            <s:TextInput id="userNameTI"/>
        </mx:FormItem>
        <mx:FormItem label="Location:">
            <s:TextInput id="locationTI"/>
        </mx:FormItem>
    </mx:Form>
    <s:Button id="addBtn" label="Add" click="addBtn_clickHandler(event)"/>
    <mx:DataGrid id="dg" width="100%" dataProvider="{ac}"/>
</s:WindowedApplication>