简体   繁体   English

如何设置用于列表控件拖放的“格式”?

[英]How do I set the “format” used for drag'n'drop on a list control?

I'm dragging from a TileList to a custom component. 我正在从TileList拖动到自定义组件。 I want to know what is being dragged before I accept it. 在接受之前,我想知道正在拖动什么。 How do I set the "format" that is used for "event.dragSource.formats" in the DragEvent? 如何在DragEvent中设置用于“ event.dragSource.formats”的“格式”?

Edit for clarification: When you set "dragEnabled=true" on the TileList, it takes care of the drag source stuff, but it uses "items" as the format for the DragEvent. 编辑以澄清问题:在TileList上设置“ dragEnabled = true”时,它会处理拖动源内容,但是它将“项目”用作DragEvent的格式。 I'm looking for a way to have the TileList use the correct format. 我正在寻找让TileList使用正确格式的方法。

Hey, this is a great question, and quite complex. 嘿,这是一个很大的问题,非常复杂。 Because Flex is primarily developed by Adobe, they don't have the power/resources/money to cover edge/customization cases like this. 因为Flex主要是由Adobe开发的,所以它们没有能力/资源/金钱来涵盖这样的边缘/定制案例。 If only they decentralized Flex's development! 如果仅是他们分散了Flex的开发!

I've had to deal with this problem too. 我也不得不处理这个问题。 It boils down to the fact Flex has hard-coded specific data source 'types' into the ListBase source , so you can't change the types. 归结为事实,即Flex已将特定的数据源“类型”硬编码到ListBase源中 ,因此您不能更改类型。 That's good and bad... Check out all the drag[Type]Handler methods in that ListBase class and see what's going on. 那是好事还是坏事...检查该ListBase类中的所有drag[Type]Handler方法,然后看看发生了什么。

All we need to do is intercept the DragEvent.DRAG_START event and call event.stopImmediatePropagation() (luckily flex will listen to that!). 我们需要做的就是拦截DragEvent.DRAG_START事件并调用event.stopImmediatePropagation() (幸运的是flex会监听!)。 Here's an example app: 这是一个示例应用程序:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[

            import mx.events.DragEvent;
            import mx.managers.DragManager;

            protected function updateDragSource():void
            {
                selected = !selected; // flip;
                sourceType = selected ? "mySource" : "items";;
            }

            protected function dragEnterHandler(event:DragEvent):void
            {
                if (!event.dragSource.hasFormat(sourceType))
                    event.stopImmediatePropagation();
            }

        ]]>
    </mx:Script>

    <mx:Boolean id="selected"/>
    <mx:String id="sourceType">items</mx:String>

    <mx:TileList id="list" width="100%" height="100%" labelField="name"
        dragEnabled="true" dropEnabled="true" dragMoveEnabled="true"
        dragEnter="dragEnterHandler(event)">
        <mx:dataProvider>
            <mx:ArrayCollection>
                <mx:Object name="one"/>
                <mx:Object name="two"/>
                <mx:Object name="three"/>
                <mx:Object name="four"/>
            </mx:ArrayCollection>
        </mx:dataProvider>
    </mx:TileList>

    <mx:Label text="Change Drag Source Type:"/>
    <mx:Button id="button" click="updateDragSource()" label="{sourceType}"/>
</mx:Application>

This gives you the base for checking if dragSource.hasFormat returns true/false. 这为您提供了检查dragSource.hasFormat返回true / false的基础。 If you want to change what the dragSource's format is , you're going to have to extend TileList and override all of the drag methods :/. 如果你想改变的DragSource的格式什么,你将不得不延长的TileList并覆盖所有的阻力方法:/。 The items and orderedItems are hard-coded into ListBase, so you can't change the format easily. 这些itemsorderedItems被硬编码到ListBase中,因此您无法轻松更改格式。

The only way you can use your own formats is to not use any of the ListBase-extending classes, and implement your own drag/drop system. 可以使用自己的格式的唯一方法是不使用任何ListBase扩展类,而是实现自己的拖放系统。 It's not that bad. 没那么糟糕。 The reason is because, if you look into all the drag event handlers in ListBase, they have things like this: 原因是,如果查看ListBase中的所有拖动事件处理程序,它们将具有以下内容:

var dragSource:DragSource = event.dragSource;
if (!dragSource.hasFormat("items") && !dragSource.hasFormat("orderedItems"))
    return;

So if they're not that format, it won't allow you to drag. 因此,如果它们不是该格式,则不允许您拖动。

Hope that helps, Lance 希望有帮助,兰斯

the formats property is an array, so you would use array functions to access it. formats属性是一个数组,因此您将使用数组函数来访问它。 I think its more something you want to read. 我认为这是您想要阅读的更多内容。 so your drop handler would be a statment like 因此,您的掉落处理程序将像

if formats is "a tile list item" then do the drop, else deny the drop 如果format是“拼贴列表项”,则删除,否则拒绝删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM