tags:

views:

20

answers:

1

I am implementing drag and drop from a DataGrid onto a List in a Flex 3 AIR application. I would like to have the drag image be a photo (jpg) referenced by a String field in the data grid item, named 'imagePath'. I'm having trouble getting the image to show up during dragging. I have triple checked that it is not because of an invalid path to the image. I have tried Image's source() and load() methods in every way I can think of. I am calling this method 'dragCurrentToList(event)' on a mouseDown event.

private function dragCurrentToList(event:MouseEvent):void 
{                
    var current:Object = event.currentTarget.selectedItem;

    var dragImg:Image = new Image();
    dragImg.load(current.imagePath);
    dragImg.width = 100; 
    dragImg.width = 100;

    var dsource:DragSource = new DragSource();
    dsource.addData(current, 'record');

    DragManager.doDrag(event.currentTarget as DataGrid, dsource, event, dragImg);
}

This works perfectly if I set the image source to the following bindable variable but I don't want to hardcode the image name.

[Bindable] 
[Embed(source='assets/icons/default.jpg')]
public var dragIcon:Class;

...
dragImg.source = dragIcon
...  
A: 

In your dragCurrentToList method, why are you loading the image instead of just specifying the source attribute as the URL to the image?

http://livedocs.adobe.com/flex/3/langref/mx/controls/SWFLoader.html#source

private function dragCurrentToList(event:MouseEvent):void 
{                
    var current:Object = event.currentTarget.selectedItem;

    var dragImg:Image = new Image();
    dragImg.source = current.imagePath;
    dragImg.width = 100; 
    dragImg.width = 100;

    var dsource:DragSource = new DragSource();
    dsource.addData(current, 'record');

    DragManager.doDrag(event.currentTarget as DataGrid, dsource, event, dragImg);
}

Also make sure you are responding to the dragStart event. ( http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html#event:dragStart ). And I believe instead of accessing the DragManager class; you should simply modify the dragSource property of the dragStart event.

www.Flextras.com
Thanks, but I've tried that. Maybe it's because imagePath is not a class? I've tried: dragImg.source = contact.imagePath; and dragImg.source = "assets/icons/default.jpg"; and dragImg.source = "@Embed(source='assets/icons/default.jpg')"They all have the same behavior. Nothing appears when dragging and nothing in the Console. Yes, I switched to responding to dragStart. Makes sense.
cafe101
I take back the portion about using dragStart per http://livedocs.adobe.com/flex/3/html/help.html?content=dragdrop_5.html
cafe101
Okay I pulled out code from the Flextras DataSorter, where I used a similar approach [we generate a bitmap data on the fly; not referencing image data from the item, though]. dragImage data is set in the dragStartHandler using the DragManager.doDrag function. But, dragImage is a getter. Is is possible you can override the dragImage getter w/o mucking w/ the dragStartEvent. BUt, if you do decide to use the dragEvent, be sure to override the existing dragStartHandler so that you don't have two competing sections of code trying to set the dragImage.
www.Flextras.com