/// <summary> /// 拖拽结束 /// </summary> protected void DragFinished() { if (_adorner != null) { AdornerLayer.GetAdornerLayer(_source).Remove(_adorner); } _adorner = null; }
/// <summary> /// 执行拖拽操作,在进行了有效的拖拽操作时执行 /// </summary> /// <param name="dragItem"></param> /// <param name="adornerSource"></param> private void DoDragDrop(object dragItem, UIElement adornerSource) { if (adornerSource != null) //如果选中按钮不为空 { var rect = VisualTreeHelper.GetDescendantBounds(adornerSource); //获取拖拽节点的外边框 var size = new Size((double)Math.Ceiling(rect.Width), (double)Math.Ceiling(rect.Height)); //获取比节点大的整数值边框 var brush = new VisualBrush(adornerSource); //初始化包含节点的VisualBrush _adorner = new DragDropAdorner(_dragScope, size, brush); //新建拖拽装饰器 _adorner.Opacity = 0.7; _adorner.Visibility = Visibility.Hidden; } DragDrop.AddPreviewDragEnterHandler(_dragScope, _dragEnter); DragDrop.AddPreviewDragOverHandler(_dragScope, _dragOver); DragDrop.AddPreviewDragLeaveHandler(_dragScope, _dragLeave); DragDrop.DoDragDrop(_source, dragItem, DragDropEffects.All); //执行拖拽方法,该方法会在拖拽结束后结束并返回 DragFinished(); //执行拖拽结束方法 DragDrop.RemovePreviewDragEnterHandler(_dragScope, _dragEnter); DragDrop.RemovePreviewDragOverHandler(_dragScope, _dragOver); DragDrop.RemovePreviewDragLeaveHandler(_dragScope, _dragLeave); }