Flex中实现double-click修改DataGrid

转自http://blog.chinaunix.net/u/21684/showart_971710.html
Alex’s Flex Closet中实现用双击的方法来修改datagrid中的item。因为在datagrid默认的修改的是单击动作。
代码如下:

package
{
import flash.events.MouseEvent;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.EventPriority;
import mx.events.DataGridEvent;

/**
* DataGrid that only allows editing if you double click
*/
public class DoubleClickDataGrid extends DataGrid
{

    public function DoubleClickDataGrid()
    {
        super();
        doubleClickEnabled = true;
    }

    override protected function mouseDoubleClickHandler(event:MouseEvent):void
    {
        var dataGridEvent:DataGridEvent;
        var r:IListItemRenderer;
        var dgColumn:DataGridColumn;

        r = mouseEventToItemRenderer(event);
        if (r && r != itemEditorInstance)
        {
            var dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);
            if (columns[dilr.listData.columnIndex].editable)
            {
                dgColumn = columns[dilr.listData.columnIndex];
                dataGridEvent = new DataGridEvent(DataGridEvent.ITEM_EDIT_BEGINNING, false, true);
                // ITEM_EDIT events are cancelable

                dataGridEvent.columnIndex = dilr.listData.columnIndex;
                dataGridEvent.dataField = dgColumn.dataField;
                dataGridEvent.rowIndex = dilr.listData.rowIndex + verticalScrollPosition;
                dataGridEvent.itemRenderer = r;
                dispatchEvent(dataGridEvent);
            }
        }

        super.mouseDoubleClickHandler(event);
    }

    override protected function mouseUpHandler(event:MouseEvent):void
    {
        var r:IListItemRenderer;
        var dgColumn:DataGridColumn;

        r = mouseEventToItemRenderer(event);
        if (r)
        {
            var dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);
            if (columns[dilr.listData.columnIndex].editable)
            {
                dgColumn = columns[dilr.listData.columnIndex];
                dgColumn.editable = false;
            }
        }

        super.mouseUpHandler(event);

        if (dgColumn)
            dgColumn.editable = true;
    }

}

}


主程序:



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" verticalGap="0" xmlns:local="*" height="500" >
    <mx:Script>
    <![CDATA[
        private var arr:Array = [
            { firstName: "Alex", lastName: "Harui" },
            { firstName: "Gordon", lastName: "Smith" },
            { firstName: "Deepa", lastName: "Subramanian" },
            { firstName: "Matt", lastName: "Chotin" },
            { firstName: "Ely", lastName: "Greenfield" },
            { firstName: "Kevin", lastName: "Lynch" },
            { firstName: "Shantanu", lastName: "Narayan" },
            { firstName: "Joan", lastName: "Lafferty" },
            { firstName: "Ryan", lastName: "Frishberg" },
            { firstName: "Jason", lastName: "Szeto" },
            { firstName: "Mark", lastName: "Anders" },
            { firstName: "Peter", lastName: "Flynn" },
            { firstName: "Heidi", lastName: "Williams" }

        ];

    ]]>
    </mx:Script>
    <local:DoubleClickDataGrid id="dg" initialize="dg.dataProvider=arr" editable="true">
        <local:columns>
            <mx:DataGridColumn dataField="firstName" headerText="First Name" />
            <mx:DataGridColumn dataField="lastName" headerText="Last Name" />
        </local:columns>
    </local:DoubleClickDataGrid>
</mx:Application>

你可能感兴趣的:(xml,.net,Flex,Blog,Flash)