【发布时间】:2023-03-26 20:38:01
【问题描述】:
我的画布中有一些自定义控件。
这些控件可以通过拖放来移动,也可以通过单击来选择。
现在,我实现了这样的拖放功能:
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnPreviewMouseLeftButtonDown(e);
this.isDragInProgress = false;
// Cache the mouse cursor location.
this.origCursorLocation = e.GetPosition(this);
// Walk up the visual tree from the element that was clicked,
// looking for an element that is a direct child of the Canvas.
var source = e.Source;
var element = this.FindCanvasChild(source as DependencyObject);
if (element == null || !(element is MyControl))
return;
this.ElementBeingDragged = element;
// Get the element's offsets from the four sides of the Canvas.
this.draggedLeft = Canvas.GetLeft(this.ElementBeingDragged);
this.darggedTop = Canvas.GetTop(this.ElementBeingDragged);
// Set the Handled flag so that a control being dragged
// does not react to the mouse input.
e.Handled = true;
this.isDragInProgress = true;
}
现在,我的问题是我无法选择 MyControl 点击它...(自定义控件上没有 MouseClick 事件,MouseDown 现在也不起作用..)
如果我评论e.Handled = true;,控件将在拖动时改变它的选择,如果不评论它,控件根本不会改变它的选择....(
【问题讨论】:
标签: .net wpf wpf-controls