添加项目文件。
This commit is contained in:
35
SamplePreSystem.UI/Behaviors/BorderMouseMoveBehavior.cs
Normal file
35
SamplePreSystem.UI/Behaviors/BorderMouseMoveBehavior.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
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;
|
||||
|
||||
namespace SamplePreSystem.UI.Behaviors
|
||||
{
|
||||
public class BorderMouseMoveBehavior :Behavior<Border>
|
||||
{
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
//base.OnAttached();
|
||||
|
||||
AssociatedObject.MouseMove += AssociatedObject_MouseMove;
|
||||
}
|
||||
|
||||
private void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
// 获取窗口并执行拖拽
|
||||
if (Window.GetWindow(AssociatedObject) is Window window)
|
||||
{
|
||||
window.DragMove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
90
SamplePreSystem.UI/Behaviors/DataGridDragBehavior.cs
Normal file
90
SamplePreSystem.UI/Behaviors/DataGridDragBehavior.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
using SamplePre.Models;
|
||||
using SamplePreSystem.UI.ViewModel.SopManager;
|
||||
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;
|
||||
|
||||
namespace SamplePreSystem.UI.Behaviors
|
||||
{
|
||||
public class DataGridDragBehavior :Behavior<DataGrid>
|
||||
{
|
||||
protected override void OnAttached()
|
||||
{
|
||||
//拖拽进入
|
||||
AssociatedObject.DragEnter += AssociatedObject_DragEnter;
|
||||
|
||||
AssociatedObject.Drop += AssociatedObject_Drop;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public ICommand DropCompletedCommand
|
||||
{
|
||||
get { return (ICommand)GetValue(DropCompletedCommandProperty); }
|
||||
set { SetValue(DropCompletedCommandProperty, value); }
|
||||
}
|
||||
|
||||
// Using a DependencyProperty as the backing store for DropCompletedCommand. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty DropCompletedCommandProperty =
|
||||
DependencyProperty.Register("DropCompletedCommand", typeof(ICommand), typeof(DataGridDragBehavior), new PropertyMetadata(null));
|
||||
|
||||
|
||||
|
||||
//// 可选:传递参数(比如拖拽后的数据源、拖拽前后索引)
|
||||
//public static readonly DependencyProperty DropCommandParameterProperty =
|
||||
// DependencyProperty.Register(
|
||||
// nameof(DropCommandParameter),
|
||||
// typeof(object),
|
||||
// typeof(DataGridDragBehavior));
|
||||
|
||||
//public object DropCommandParameter
|
||||
//{
|
||||
// get => GetValue(DropCommandParameterProperty);
|
||||
// set => SetValue(DropCommandParameterProperty, value);
|
||||
//}
|
||||
|
||||
|
||||
|
||||
private void AssociatedObject_Drop(object sender, DragEventArgs e)
|
||||
{
|
||||
|
||||
// 获取拖拽的数据
|
||||
if (e.Data.GetData(typeof(TreeListItem)) is TreeListItem dropItem)
|
||||
{
|
||||
|
||||
if (dropItem == null) return;
|
||||
|
||||
//var dataContext = this.DataContext as SopMangerViewModel;
|
||||
//dataContext.AddDragSopAction(dropItem);
|
||||
if (DropCompletedCommand != null && DropCompletedCommand.CanExecute(dropItem))
|
||||
{
|
||||
DropCompletedCommand.Execute(dropItem);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void AssociatedObject_DragEnter(object sender, System.Windows.DragEventArgs e)
|
||||
{
|
||||
// 验证拖拽数据类型是否是我们需要的TreeItemModel
|
||||
if (!e.Data.GetDataPresent(typeof(TreeListItem)))
|
||||
{
|
||||
e.Effects = DragDropEffects.None;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
85
SamplePreSystem.UI/Behaviors/TreeViewDragBehavior.cs
Normal file
85
SamplePreSystem.UI/Behaviors/TreeViewDragBehavior.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user