在WPF中选择和拖动之间有所区别

时间:2021-07-25 11:50:54

I have some custom controls in my Canvas.

我的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..)

现在,我的问题是我无法选择MyControl点击它...(自定义控件上没有MouseClick事件,现在MouseDown也没有工作..)

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.... (

如果我要评论e.Handled = true;拖动时控件会改变它的选择,如果不对它进行评论,控件根本不会改变选择....(

2 个解决方案

#1


5  

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 and SystemParameters.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.

而不是在MouseDown处理程序中开始拖动操作,您可以保存一些初始状态,而是提交拖动MouseMove处理程序,您可以在其中检查SystemParameters.MinimumHorizo​​ntalDragDistance和SystemParameters.MinimumVerticalDragDistance以查看是否有足够的移动来开始拖动操作。然后,您可以在MouseUp处理程序中包含代码以完成拖动操作,或者如果由于移动太小而从未启动过,则执行select操作。

#2


0  

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.

在MouseMove事件处理程序中,有一些代码可以测试用户拖动超过阈值距离,当发生这种情况时,启动拖动操作。

http://www.codeproject.com/KB/WPF/SimpleDragSelection.aspx

http://www.codeproject.com/KB/WPF/SimpleDragSelection.aspx

#1


5  

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 and SystemParameters.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.

而不是在MouseDown处理程序中开始拖动操作,您可以保存一些初始状态,而是提交拖动MouseMove处理程序,您可以在其中检查SystemParameters.MinimumHorizo​​ntalDragDistance和SystemParameters.MinimumVerticalDragDistance以查看是否有足够的移动来开始拖动操作。然后,您可以在MouseUp处理程序中包含代码以完成拖动操作,或者如果由于移动太小而从未启动过,则执行select操作。

#2


0  

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.

在MouseMove事件处理程序中,有一些代码可以测试用户拖动超过阈值距离,当发生这种情况时,启动拖动操作。

http://www.codeproject.com/KB/WPF/SimpleDragSelection.aspx

http://www.codeproject.com/KB/WPF/SimpleDragSelection.aspx