WPF 中选择和拖动的区别
我的画布中有一些自定义控件。
该控件可以通过拖放移动,或通过单击选择。
现在,我实现了像这样的拖放操作:
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;
控件在拖动时会改变它的选择,如果不注释它,控件根本不会改变它的选择....(
I have some custom controls in my Canvas.
That controls can be moved by drag and drop, or selected by click.
Now, I implemented the Drag and Drop something like this:
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;
}
Now, my problem is that I can't select MyControl clicking on it... (there is no MouseClick event on the custom Control, nor MouseDown works now..)
If I'll comment e.Handled = true;
the control will change it selection when dragging, if don't comment it, the control will not change it selection at all.... (
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以保存一些初始状态,然后在 MouseMove 处理程序中提交拖动,而不是在 MouseDown 处理程序中开始拖动操作,您可以在其中检查 SystemParameters.MinimumHorizontalDragDistance 和 SystemParameters.MinimumVerticalDragDistance /code> 查看是否有足够的移动来开始拖动操作。然后,您可以在 MouseUp 处理程序中包含代码以完成拖动操作,或者如果由于移动太小而从未开始,则改为执行选择。
Rather than beginning the drag operation in a MouseDown handler, you can save some initial state and instead commit to dragging in a MouseMove handler, where you can check against
SystemParameters.MinimumHorizontalDragDistance
andSystemParameters.MinimumVerticalDragDistance
to see if there has been enough movement to begin a drag operation. You can then include code in a MouseUp handler to either complete the drag operation or, if it never started because the movement was too small, do a select instead.我刚刚写了一篇代码项目文章,可能会对您有所帮助。本文介绍的是拖动选择和多项目拖动。
在 MouseMove 事件处理程序中,有一些代码用于测试用户拖动的距离是否超过阈值距离,当发生这种情况时,将启动拖动操作。
http://www.codeproject.com/KB/WPF/SimpleDragSelection.aspx
I have just written a code project article that might help you. The article is about drag selection and multiple item dragging.
In the MouseMove event handler there is code that tests for the user dragging further than the threshold distance, when this happens the drag operation is initiated.
http://www.codeproject.com/KB/WPF/SimpleDragSelection.aspx