好吧,明白了,谢谢Robusto,你的提示#2是灵感,但是我不得不使用鼠标放下的监听器 - 选择事件触发得太晚。
在下面的示例中,我使用的是我的some other question的代码。
此示例,您可以在List或DataGrid拖到仅第一项:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" minWidth="955" minHeight="600">
<mx:List id="list" dataProvider="{['Tom','Jerry', 'Amy', 'Betty', 'Chris', 'Dean', 'Email', 'Floyd', 'Grant', 'Helen', 'Iris', 'Jack']}" minWidth="200"
mouseDown="onMouseDown(event)"
/>
<mx:DataGrid id="dg" dataProvider="{[{title:'Tom'},{title:'Jerry'}]}" minWidth="200"
mouseDown="onMouseDown(event)"
>
<mx:columns>
<mx:DataGridColumn dataField="title" />
</mx:columns>
</mx:DataGrid>
<mx:Script>
<![CDATA[
import mx.controls.listClasses.ListBase;
import mx.events.DragEvent;
protected function onMouseDown(event:MouseEvent):void
{
var listBaseComp:ListBase = ListBase(event.currentTarget);
var clickIndex:int = this.findClickedItemIndex(event.stageX, event.stageY, listBaseComp);
listBaseComp.dragEnabled = clickIndex == 0;
}
/**
* Returns a dataProvider item that displays at the given coords for the given dataGrid.
* Code provided by Stackoverflow user https://stackoverflow.com/users/165297/amarghosh,
* thanks a lot!
*/
protected function findClickedItemIndex(globalX:Number, globalY:Number, listComp:ListBase):int
{
var p1 : Point;
var p2 : Point;
var renderer : DisplayObject;
for(var i:int=0; i<listComp.dataProvider.length; i++) {
renderer = DisplayObject(listComp.indexToItemRenderer(i));
if (!renderer) //item is not displayed (scroll to view it)
continue;
p1 = new Point(renderer.x, renderer.y);
p2 = new Point(renderer.width, renderer.height);
p1 = renderer.parent.localToGlobal(p1);
p2 = renderer.localToGlobal(p2);
if(globalX >= p1.x && globalX <= p2.x && globalY >= p1.y && globalY <= p2.y)
return i;
}
return -1;
}
]]>
</mx:Script>
</mx:Application>
来源
2010-03-23 09:08:33
Tom
不错的选择Robusto,你有使用/测试过他们吗? 1似乎非常万无一失,但视觉上很烦人。 2看起来好像会起作用,但很多看起来应该为我工作的东西都不行。 – invertedSpear 2010-03-22 17:02:10
我已经使用过他们,他们为我的公司工作。请注意,#1使用List的disabledFunction属性,而#2从dragDrop事件调用处理程序,该事件检查行是否有效拖动,如果该行无效,则调用event.stopImmediatePropagation()和event.preventDefault() 。 – Robusto 2010-03-22 19:06:31