如何在wpf中检测鼠标何时滚动
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你真的必须将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.