【问题标题】:Make difference between Select and Drag in WPFWPF中选择和拖动的区别
【发布时间】: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


    【解决方案1】:

    除了在 MouseDown 处理程序中开始拖动操作之外,您还可以保存一些初始状态并改为在 MouseMove 处理程序中进行拖动,您可以在其中检查 SystemParameters.MinimumHorizontalDragDistanceSystemParameters.MinimumVerticalDragDistance 以查看是否有足够的移动开始拖动操作。然后,您可以在 MouseUp 处理程序中包含代码以完成拖动操作,或者,如果由于移动太小而无法启动,则改为执行选择。

    【讨论】:

      【解决方案2】:

      我刚刚写了一篇可能对您有所帮助的代码项目文章。这篇文章是关于拖动选择和多项目拖动的。

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

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

      【讨论】:

        猜你喜欢
        • 2015-05-20
        • 1970-01-01
        • 2011-10-11
        • 2011-01-26
        • 2010-12-22
        • 1970-01-01
        • 2011-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多