目的:只允許同級拖動。
兩個判斷:
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 = 0,
AsChild = 1,
Before = 2,
After = 3
}
}