目的:只允许同级拖动。
两个判断:
1.原节点(假设为:S)的父级如果不等于目标节点(假设为:T)的父节点,那么发生了跨级,即非同级移动。这个判断很容易。
2.S、T是同一级的,但是S是移动到T下一级,这种情景下,移动过程中,S和T的父节点是一致的,不能判断是否跨级移动,那么怎么办判断呢?
方案1:在afterDrop事件中来判断父节点是否一致,因为移动已经完成,父节点发什么了变化,根据判断结果然后再把节点恢复回去。这种做法很low。
方案2:在移动过程中判断S被移动到T节点的位置:T节点前、T节点后、T节点下,如果是移动到T节点下,那么禁止移动即可。
下面贴出方案2判断方法:
/// <summary>
/// 获取拖动过程中的方向
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <returns></returns>
private DragInsertPosition AjustDirection(object sender, DragEventArgs e)
{
TreeListNode dragNode, targetNode;
TreeList tl = sender as TreeList;
Point p = tl.PointToClient(new Point(e.X, e.Y));
dragNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode;
TreeListHitInfo hit = tl.CalcHitInfo(p);
PropertyInfo pi = typeof(TreeList).GetProperty("Handler", BindingFlags.Instance | BindingFlags.NonPublic);
TreeListHandler handler = (TreeListHandler)pi.GetValue(tl, null);
return handler.StateData.DragInfo.DragInsertPosition; }
private void treeListNav_DragOver(object sender, DragEventArgs e)
{
TreeListNode dragNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode;
System.Diagnostics.Debug.WriteLine("Over:" + e.Effect);
TreeListNode targetNode;
Point p = treeListNav.PointToClient(MousePosition);
targetNode = treeListNav.CalcHitInfo(p).Node;
if (targetNode == null)
{
return;
}
FileContent tagParent = null;//拖动后的父级数据
if (targetNode.ParentNode != null)
{
tagParent = this.treeListNav.GetRow(targetNode.ParentNode.Id) as FileContent;
}
if (sourceParent != tagParent)//发生跨级拖动
{
// MessageHelper.ShowHit("只能在同一级拖动,移动未成功。");
e.Effect = DragDropEffects.None;
return;
} //移动到了同级子节点下
if (AjustDirection(sender, e) == DragInsertPosition.AsChild)
{
e.Effect = DragDropEffects.None;
return;
} if (e.Effect == DragDropEffects.Link)
{
// MessageHelper.ShowHit("不能移动到子集。");
e.Effect = DragDropEffects.None;
} }
这个确定移动方向的枚举:
namespace DevExpress.XtraTreeList
{
public enum DragInsertPosition
{
None = ,
AsChild = ,
Before = ,
After =
}
}