1.2关于拖拽
树节点的拖拽事件,一般用三个,dragDrop,dragEnter,dragOver分别是有拖拽趋向时发生,拖拽了以后发生,拖拽动作结束发生。以下是我写的拖拽事件。
仅供参考,
//关于拖拽事件还有一个属性,optionsbehavior.dragnodes如果等于 false就是不允许拖拽了
#region drag 事件
private void treeDepartment_DragDrop(object sender, DragEventArgs e)
{
TreeListNode targetNode =GetNodeByLocation(this, new Point(e.X, e.Y));
bool succeed = DoDragDrop(e, targetNode);
if (succeed && !GetDraggedNode(e).TreeList.Equals(this))
{
this.Refresh();
}
}
private void treeDepartment_DragEnter(object sender, DragEventArgs e)
{
UiCommon.SetDragEffect(e, GetNodeByLocation(this, new Point(e.X, e.Y)));
}
private void treeDepartment_DragOver(object sender, DragEventArgs e)
{
UiCommon.SetDragEffect(e, GetNodeByLocation(this, new Point(e.X, e.Y)));
}
#endregion
/// <summary>
/// 根据鼠标位置获取节点
/// </summary>
/// <param name="treeList">节点所在的treelist</param>
/// <param name="location">节点位置</param>
/// <returns></returns>
private TreeListNode GetNodeByLocation(TreeList treeList, Point location)
{
TreeListHitInfo hitInfo = treeList.CalcHitInfo(treeList.PointToClient(location));
return hitInfo.Node;
}
/// <summary>
/// 拖拽事件
/// </summary>
/// <param name="e"></param>
/// <param name="targetNode"></param>
/// <returns></returns>
private bool DoDragDrop(DragEventArgs e, TreeListNode targetNode)
{
if (e.Effect != DragDropEffects.Move)
return false;
TreeListNode draggedNode = GetDraggedNode(e);
DataRow draggedRow = draggedNode.Tag as DataRow;
if (draggedRow == null)
return false;
DataRow targetRow = targetNode.Tag as DataRow;
if (targetRow == null)
return false;
string tableName = draggedRow.Table.TableName;
return DragDepartment(draggedRow, targetRow);
}
// <summary>
/// 移动部门位置
/// </summary>
/// <param name="draggedRow"></param>
/// <param name="targetRow"></param>
/// <returns></returns>
private static bool DragDepartment(DataRow draggedRow, DataRow targetRow)
{
string departmentName = "";
try
{
string targetTableName = targetRow.Table.TableName;
departmentName = draggedRow["DeptName"].ToString();
if (!targetTableName.Equals("DepartmentTable", StringComparison.CurrentCultureIgnoreCase))
return false;
DepartmentInfoAccessor.MoveDepartment(draggedRow["DeptID"].ToString(),
targetRow["DeptID"].ToString());
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 设置拖拽时的鼠标效果
/// </summary>
/// <param name="e"></param>
/// <param name="targetNode"></param>
internal static void SetDragEffect(DragEventArgs e, TreeListNode targetNode)
{
TreeListNode draggedNode = GetDraggedNode(e);
DataRow draggedRow = (DataRow)draggedNode.Tag;
//当被拖动的节点无法处理时显示禁止图标
if (draggedRow == null ||
(!draggedRow.Table.TableName.Equals("UserTable", StringComparison.CurrentCultureIgnoreCase) &&
!draggedRow.Table.TableName.Equals("DepartmentTable", StringComparison.CurrentCultureIgnoreCase)))
{
e.Effect = DragDropEffects.None;
return;
}
//当不存在目标节点或目标节点的层级大于等于被拖动节点时(后者仅针对树内拖拽),显示禁止图标
if (targetNode == null || draggedNode == targetNode || targetNode.HasAsParent(draggedNode))
{
e.Effect = DragDropEffects.None;
return;
}
e.Effect = DragDropEffects.Move;
}