如何在flex 4中创建拖拽面板?

时间:2011-02-23 18:09:00

标签: flash flex drag-and-drop

我正在尝试拖动一个组件并在drop上我希望它创建一个面板。这是我的动作脚本,但它似乎不起作用,任何想法为什么?

private function dragDrop(e:DragEvent): void {
    var userPanel:Panel = new Panel();
    userPanel.width = 100;
    userPanel.height = 100;
    userPanel.x = 10;
    userPanel.y = 10;
    userPanel.visible = true;
    addChild(userPanel);
}

2 个答案:

答案 0 :(得分:1)

您包含的代码有效。是否正确配置了拖动启动器?是否将放置目标配置为接受拖放?

这是一些代码,当canvas2被拖入canvas1时,它会向canvas1添加一个面板:

protected function canvas2_dragStartHandler(event:MouseEvent):void {
    var dragInitiator:Canvas=Canvas(event.currentTarget);
    var ds:DragSource = new DragSource();              

    DragManager.doDrag(dragInitiator, ds, event);
}

protected function canvas1_dragEnterHandler(event:DragEvent):void {
    DragManager.acceptDragDrop(Canvas(event.currentTarget));
}

protected function canvas1_dragDropHandler(event:DragEvent):void {
    var userPanel:Panel = new Panel();
    userPanel.width = 100;
    userPanel.height = 100;
    userPanel.x = 10;
    userPanel.y = 10;
    userPanel.visible = true;
    Canvas(event.currentTarget).addChild(userPanel);
}

答案 1 :(得分:0)

酷让它工作......所以我缺少的步骤是接受dragEnter事件的拖动,这里是完整的代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.core.IUIComponent;
            import mx.events.DragEvent;
            import mx.managers.DragManager;

            import spark.components.Panel;
            private function dragDrop(e:DragEvent): void {
                var userPanel:Panel = new Panel();
                userPanel.width = 100;
                userPanel.height = 100;
                userPanel.x = 10;
                userPanel.y = 10;
                userPanel.visible = true;
                addElement(userPanel);
            }


            protected function canvas1_dragEnterHandler(event:DragEvent):void
            {
                // TODO Auto-generated method stub
                DragManager.acceptDragDrop(event.currentTarget as IUIComponent);
            }

        ]]>
    </fx:Script>
    <mx:Canvas dragEnter="canvas1_dragEnterHandler(event)" dragDrop="dragDrop(event)" width="100%" height="100%" backgroundColor="blue"/>
    <s:List dataProvider="{new ArrayCollection(['a','item','in','here'])}" dragEnabled="true"/>
</s:Application>

肖恩