1.在不同列表类控件之间移动和复制数据示例: <?xml version="1.0" encoding="utf-8"?> <!-- dragdrop/DandDListToDG.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp();"> <mx:Script> <!--[CDATA[ import mx.events.DragEvent; import mx.managers.DragManager; import mx.core.DragSource; import mx.collections.IList; import mx.collections.ArrayCollection; private function initApp():void { srcList.dataProvider = new ArrayCollection([ {label:"第一列", data:"1"}, {label:"第二列", data:"2"}, {label:"第三列", data:"3"}, {label:"第四列", data:"4"}, ]); destDG.dataProvider = new ArrayCollection([]); } private function dragDropHandler(event:DragEvent):void { if (event.dragSource.hasFormat("items")) { // 定义自处理dragDrop事件。 event.preventDefault(); event.currentTarget.hideDropFeedback(event); // 得到放置目标。 var dropTarget:DataGrid = DataGrid(event.currentTarget); var itemsArray:Array = event.dragSource.dataForFormat('items') as Array; var tempItem:Object = { label: itemsArray[0].label, data: itemsArray[0].data, date: new Date() }; // 得到在放置目标上的放置位置。 var dropLoc:int = dropTarget.calculateDropIndex(event); IList(dropTarget.dataProvider).addItemAt(tempItem, dropLoc); } } ]]--> </mx:Script> <mx:Panel title="不同类型控件中复制移动数据示例" width="90%" height="90%" horizontalAlign="center" verticalAlign="middle"> <mx:HBox> <mx:List id="srcList" dragEnabled="true" dragMoveEnabled="true"/> <mx:DataGrid id="destDG" dropEnabled="true" dragDrop="dragDropHandler(event);"> <mx:columns> <mx:DataGridColumn headerText="名称" dataField="label"/> <mx:DataGridColumn headerText="数据" dataField="data"/> <mx:DataGridColumn headerText="日期" dataField="date"/> </mx:columns> </mx:DataGrid> </mx:HBox> </mx:Panel> </mx:Application> 2.不同类型的非列表类控件之间移动或复制数据示例: <?xml version="1.0" encoding="utf-8"?> <!-- dragdrop/DandDImageCopyMove.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal"> <mx:Script> <!--[CDATA[ import mx.managers.DragManager; import mx.core.DragSource; import mx.events.DragEvent; import flash.events.MouseEvent; [Embed(source='./assets/eek.gif')] public var globeImage:Class; // mouseMove数据管理用来定义图片控件的功能。 private function mouseOverHandler(event:MouseEvent):void { var dragInitiator:Image=Image(event.currentTarget); var ds:DragSource = new DragSource(); ds.addData(dragInitiator, "img"); // 拖动管理器所以图片作为拖动代理。 var imageProxy:Image = new Image(); imageProxy.source = globeImage; imageProxy.height=10; imageProxy.width=10; DragManager.doDrag(dragInitiator, ds, event, imageProxy, -15, -15, 1.00); } // dragEnter事件管理器用来定义放置目标。 private function dragEnterHandler(event:DragEvent):void { if (event.dragSource.hasFormat("img")) DragManager.acceptDragDrop(Canvas(event.currentTarget)); } // dragOver事件管理器设定拖放操作为复制还是移动。 // 复制或移动会影响在dragComplete事件管理器中的操作。 private function dragOverHandler(event:DragEvent):void { if (event.dragSource.hasFormat("img")) { if (event.ctrlKey) { DragManager.showFeedback(DragManager.COPY); return; } else { DragManager.showFeedback(DragManager.MOVE); return; } } DragManager.showFeedback(DragManager.NONE); } // dragDrop事件管理器设定图片控件在目标容器中的位置。 private function dragDropHandler(event:DragEvent):void { if (event.dragSource.hasFormat("img")) { var draggedImage:Image = event.dragSource.dataForFormat('img') as Image; var dropCanvas:Canvas = event.currentTarget as Canvas; var newImage:Image=new Image(); newImage.source = draggedImage.source; newImage.x = dropCanvas.mouseX; newImage.y = dropCanvas.mouseY; dropCanvas.addChild(newImage); } } // dragComplete事件管理器用来完成复制或移动图片。 private function dragCompleteHandler(event:DragEvent):void { var draggedImage:Image = event.dragInitiator as Image; var dragInitCanvas:Canvas = event.dragInitiator.parent as Canvas; if (event.action == DragManager.MOVE) dragInitCanvas.removeChild(draggedImage); } ]]--> </mx:Script> <mx:Panel title="不同类型的非列表类控件中复制移动图片示例" width="90%" height="90%" horizontalAlign="center" verticalAlign="middle"> <mx:HBox width="100%" height="100%"> <mx:Canvas width="200" height="200" borderStyle="solid" backgroundColor="#FFF6D5"> <mx:Image id="myimg" source="@Embed(source='./assets/eek.gif')" mouseMove="mouseOverHandler(event);" dragComplete="dragCompleteHandler(event);"/> </mx:Canvas> <mx:Canvas width="200" height="200" borderStyle="solid" backgroundColor="#FEDEF1" dragEnter="dragEnterHandler(event);" dragOver="dragOverHandler(event);" dragDrop="dragDropHandler(event);"> </mx:Canvas> </mx:HBox> </mx:Panel> </mx:Application>