如何在wpf中检测鼠标何时滚动

发布于 2024-12-08 16:31:11 字数 1347 浏览 0 评论 0原文

我有 2 个列表框,我将项目从一个列表框拖动到另一个列表框。 问题是,当滚动在列表框中可见时,如果我单击滚动向上/向下移动,它会再次开始拖动。 有什么方法可以检测鼠标何时位于滚动区域上,以便我可以阻止它启动拖动操作?

以下是代码:

Private Sub lstbox_PreviewMouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) 'Handles lstFieldsAvailable.PreviewMouseLeftButtonDown
    _mouseDownPos = e.GetPosition(Nothing)
    _isMouseDown = True
    _mouseDownSource = sender
End Sub

Private Sub lstbox_PreviewMouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs) 'Handles lstFieldsAvailable.PreviewMouseMove
    Dim mousePos As Point = e.GetPosition(Nothing)
    Dim diff As Vector = _mouseDownPos - mousePos
    Dim lstbox As ListBox = CType(sender, ListBox)

    If _isMouseDown And e.LeftButton = MouseButtonState.Pressed And lstbox.SelectedItems.Count > 0 And _
        lstbox.IsMouseOver And _
        (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance Or _
         Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) Then

        'get the selected items
        Dim dragData As New DragDataStruct(lstbox)
        For Each item As String In lstbox.SelectedItems
            dragData.Items.Add(item)
        Next

        DragDrop.DoDragDrop(lstbox, dragData, DragDropEffects.Move)

    End If
End Sub

I have 2 listboxes and I drag items from one to the other.
Problem is that when the scroll is visible on listbox and if I click on scroll to move up/down, it starts dragging again.
Is there any way to detect when mouse is over the scroll area so I can prevent it from initiating the drag action?

Following is the code:

Private Sub lstbox_PreviewMouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) 'Handles lstFieldsAvailable.PreviewMouseLeftButtonDown
    _mouseDownPos = e.GetPosition(Nothing)
    _isMouseDown = True
    _mouseDownSource = sender
End Sub

Private Sub lstbox_PreviewMouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs) 'Handles lstFieldsAvailable.PreviewMouseMove
    Dim mousePos As Point = e.GetPosition(Nothing)
    Dim diff As Vector = _mouseDownPos - mousePos
    Dim lstbox As ListBox = CType(sender, ListBox)

    If _isMouseDown And e.LeftButton = MouseButtonState.Pressed And lstbox.SelectedItems.Count > 0 And _
        lstbox.IsMouseOver And _
        (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance Or _
         Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) Then

        'get the selected items
        Dim dragData As New DragDataStruct(lstbox)
        For Each item As String In lstbox.SelectedItems
            dragData.Items.Add(item)
        Next

        DragDrop.DoDragDrop(lstbox, dragData, DragDropEffects.Move)

    End If
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2024-12-15 16:31:11

你真的必须将ListBox设置为拖动源吗...

不能使用ListBoxItem作为拖动源吗?如果这样做,ListBoxItem 将自动从其可拖动区域中排除滚动条。

Do you really have to set the ListBox as the drag source ...

Cant you use ListBoxItem as the drag source? If you do that then ListBoxItem will automatically exclude the scrollbars from their draggable regions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文