public static class InputHelper
/// 将 MouseDown MouseMove MouseUp 封装为点击事件
/// <param name="element">要被附加的元素</param>
/// <param name="clickEventHandler">点击的事件</param>
/// <param name="dragStarted">因为拖动而结束点击时触发</param>
public static void AttachMouseDownMoveUpToClick(UIElement element, EventHandler clickEventHandler,
EventHandler dragStarted = null)
var inputInfo = GetOrCreateInputInfo(element);
inputInfo.ClickEventHandler += clickEventHandler;
inputInfo.DragStarted += dragStarted;
element.MouseDown -= Element_MouseDown;
element.MouseDown += Element_MouseDown;
element.MouseMove -= Element_MouseMove;
element.MouseMove += Element_MouseMove;
element.MouseUp -= Element_MouseUp;
element.MouseUp += Element_MouseUp;
element.LostMouseCapture -= Element_LostMouseCapture;
element.LostMouseCapture += Element_LostMouseCapture;
/// 去掉对 <paramref name="element"/> 的点击时间的监听
/// <param name="element"></param>
/// <param name="clickEventHandler">点击的事件</param>
/// <param name="dragStarted">因为拖动而结束点击时触发的事件</param>
public static void DetachMouseDownMoveUpToClick(UIElement element, EventHandler clickEventHandler,
EventHandler dragStarted = null)
var inputInfo = GetInputInfo(element);
inputInfo.ClickEventHandler -= clickEventHandler;
inputInfo.DragStarted -= dragStarted;
element.ClearValue(InputInfoProperty);
element.MouseDown -= Element_MouseDown;
element.MouseMove -= Element_MouseMove;
element.MouseUp -= Element_MouseUp;
element.LostMouseCapture -= Element_LostMouseCapture;
private static void Element_LostMouseCapture(object sender, MouseEventArgs e)
var element = (UIElement) sender;
GetInputInfo(element)?.LostCapture();
private static void Element_MouseUp(object sender, MouseButtonEventArgs e)
var element = (UIElement) sender;
GetInputInfo(element)?.Up(e.GetPosition(element));
private static void Element_MouseMove(object sender, MouseEventArgs e)
var element = (UIElement) sender;
GetInputInfo(element)?.Move(e.GetPosition(element));
private static void Element_MouseDown(object sender, MouseButtonEventArgs e)
var element = (UIElement) sender;
GetInputInfo(element)?.Down(e.GetPosition(element));
private static readonly DependencyProperty InputInfoProperty = DependencyProperty.RegisterAttached(
"InputInfo", typeof(InputInfo), typeof(InputHelper), new PropertyMetadata(default(InputInfo)));
private static InputInfo GetOrCreateInputInfo(UIElement element)
var inputInfo = GetInputInfo(element);
inputInfo = new InputInfo();
SetInputInfo(element, inputInfo);
private static void SetInputInfo(DependencyObject element, InputInfo value)
element.SetValue(InputInfoProperty, value);
private static InputInfo GetInputInfo(DependencyObject element)
return (InputInfo) element.GetValue(InputInfoProperty);
public void Down(Point position)
_downedPosition = position;
_downedTime = DateTime.Now;
public void Move(Point position)
if ((position - _downedPosition).LengthSquared > ToleranceSquared)
DragStarted?.Invoke(null, EventArgs.Empty);
public void Up(Point position)
&& (position - _downedPosition).LengthSquared <= ToleranceSquared
&& DateTime.Now - _downedTime < ClickDuringTime;
ClickEventHandler?.Invoke(null, EventArgs.Empty);
public void LostCapture()
public double ToleranceSquared { set; get; } = 0.01;
public TimeSpan ClickDuringTime { set; get; } = TimeSpan.MaxValue;
public event EventHandler ClickEventHandler;
public event EventHandler DragStarted;
public bool IsEmpty() => ClickEventHandler is null && DragStarted is null;
private Point _downedPosition;
private DateTime _downedTime;