86 lines
3.0 KiB
C#
86 lines
3.0 KiB
C#
|
|
using Microsoft.Xaml.Behaviors;
|
|||
|
|
using SamplePre.Models;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.Windows;
|
|||
|
|
using System.Windows.Controls;
|
|||
|
|
using System.Windows.Input;
|
|||
|
|
using System.Windows.Media;
|
|||
|
|
|
|||
|
|
namespace SamplePreSystem.UI.Behaviors
|
|||
|
|
{
|
|||
|
|
public class TreeViewDragBehavior :Behavior<TreeView>
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
// 拖拽起始点(用于判断是否是有效拖拽)
|
|||
|
|
private Point _dragStartPoint;
|
|||
|
|
|
|||
|
|
protected override void OnAttached()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
//记录鼠标按下时的坐标
|
|||
|
|
AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;
|
|||
|
|
|
|||
|
|
//处理TreeView的鼠标移动事件,触发拖拽
|
|||
|
|
AssociatedObject.PreviewMouseMove += AssociatedObject_PreviewMouseMove;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 处理TreeView的鼠标移动事件,触发拖拽
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="sender"></param>
|
|||
|
|
/// <param name="e"></param>
|
|||
|
|
/// <exception cref="NotImplementedException"></exception>
|
|||
|
|
private void AssociatedObject_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
|
|||
|
|
{
|
|||
|
|
// 获取当前鼠标位置
|
|||
|
|
Point currentPoint = e.GetPosition(null);
|
|||
|
|
// 计算鼠标移动的距离(过滤掉微小移动,避免误触发拖拽)
|
|||
|
|
Vector dragVector = _dragStartPoint - currentPoint;
|
|||
|
|
|
|||
|
|
// 如果鼠标左键按下,且移动距离超过阈值(系统默认的拖拽阈值),则开始拖拽
|
|||
|
|
if (e.LeftButton == MouseButtonState.Pressed &&
|
|||
|
|
(Math.Abs(dragVector.X) > SystemParameters.MinimumHorizontalDragDistance ||
|
|||
|
|
Math.Abs(dragVector.Y) > SystemParameters.MinimumVerticalDragDistance))
|
|||
|
|
{
|
|||
|
|
// 获取当前鼠标下的TreeViewItem
|
|||
|
|
var treeViewItem = FindVisualParent<TreeViewItem>(e.OriginalSource as DependencyObject);
|
|||
|
|
if (treeViewItem != null && treeViewItem.DataContext is TreeListItem dragItem)
|
|||
|
|
{
|
|||
|
|
// 开始拖拽,设置拖拽数据和拖拽效果
|
|||
|
|
DragDrop.DoDragDrop(treeViewItem, dragItem, DragDropEffects.Copy);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 查找可视化树中的父级控件(通用方法)
|
|||
|
|
/// </summary>
|
|||
|
|
private T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
|
|||
|
|
{
|
|||
|
|
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
|
|||
|
|
if (parentObject == null) return null;
|
|||
|
|
|
|||
|
|
if (parentObject is T parent)
|
|||
|
|
return parent;
|
|||
|
|
else
|
|||
|
|
return FindVisualParent<T>(parentObject);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
|
|||
|
|
{
|
|||
|
|
// 记录鼠标按下时的坐标
|
|||
|
|
_dragStartPoint = e.GetPosition(null);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|