You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.9 KiB
C#
57 lines
1.9 KiB
C#
using System;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using WpfNodeTest.Models;
|
|
|
|
namespace WpfNodeTest.Views
|
|
{
|
|
/// <summary>
|
|
/// 节点模板控件
|
|
/// </summary>
|
|
public partial class NodeTemplateControl : UserControl
|
|
{
|
|
private Point _startPoint;
|
|
private bool _isDragging;
|
|
|
|
public NodeTemplateControl()
|
|
{
|
|
InitializeComponent();
|
|
PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown;
|
|
PreviewMouseMove += OnPreviewMouseMove;
|
|
}
|
|
|
|
// 记录鼠标按下位置
|
|
private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
_startPoint = e.GetPosition(null);
|
|
_isDragging = false;
|
|
}
|
|
|
|
// 鼠标移动时启动拖放
|
|
private void OnPreviewMouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
if (e.LeftButton == MouseButtonState.Pressed && !_isDragging)
|
|
{
|
|
Point currentPosition = e.GetPosition(null);
|
|
Vector diff = _startPoint - currentPosition;
|
|
|
|
// 移动超过阈值才启动拖拽
|
|
if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
|
|
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
|
|
{
|
|
_isDragging = true;
|
|
if (DataContext is NodeTypeInfo nodeType)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"启动拖拽: {nodeType.TypeName}");
|
|
DragDrop.DoDragDrop(this, nodeType, DragDropEffects.Copy);
|
|
}
|
|
else
|
|
{
|
|
System.Diagnostics.Debug.WriteLine("DataContext不是NodeTypeInfo类型");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |