我正在开发一个Wpf项目,但是现在我遇到了ListView
问题。
事实证明我在ListView上实现了Drag&Drop
功能,工作正常。当我尝试向下或向上滚动时出现问题。通过这样做,Drag&Drop
功能被激活,阻止我保持滚动。
我发现this solution表示我们需要将控件附加到ScrollChanged
事件。
<ListView ScrollViewer.ScrollChanged="listView1_ScrollChanged"...
但我真的不知道该处理程序该怎么做。 如何禁用该事件的拖放功能?我怎么能再次启用它?或者,有没有更好的方法来解决这个问题?
这是我的Drag&Drop
代码:
private void listView1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Store the mouse position
startPoint = e.GetPosition(null);
}
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
// Get the current mouse position
Point mousePos = e.GetPosition(null);
Vector diff = startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
// Get the dragged ListViewItem
ListView listView = sender as ListView;
// Get items to drag
var a = listView.SelectedItems;
// Initialize the drag & drop operation
DataObject dragData = new DataObject("myFormat", a);
DragDrop.DoDragDrop(listView, dragData, DragDropEffects.Move);
}
}
提前致谢。
答案 0 :(得分:0)
如果您不在MouseDown事件中,则可以阻止MouseMove事件:
bool stopDrag = true;
private void listView1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Store the mouse position
startPoint = e.GetPosition(null);
stopDrag = false;
}
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
if(stopDrag)
return;
// Get the current mouse position
Point mousePos = e.GetPosition(null);
Vector diff = startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
// Get the dragged ListViewItem
ListView listView = sender as ListView;
// Get items to drag
var a = listView.SelectedItems;
// Initialize the drag & drop operation
DataObject dragData = new DataObject("myFormat", a);
DragDrop.DoDragDrop(listView, dragData, DragDropEffects.Move);
}
}
private void listView1_MouseUp(...)
{
stopDrag = true;
}
这应该有用,我在浏览器中写了这个,请原谅我,如果我做错了什么,格式化,我希望你有这个想法。
答案 1 :(得分:0)
我使用一种简单的方法来确认是否使用为此目的而制作的SystemParameters.MinimumHorizontalDragDistance
和SystemParameters.MinimumVerticalDragDistance
确认了拖动操作:
private bool IsDragConfirmed(Point point)
{
bool horizontalMovement = Math.Abs(point.X - dragStartPosition.X) >
SystemParameters.MinimumHorizontalDragDistance;
bool verticalMovement = Math.Abs(point.Y - dragStartPosition.Y) >
SystemParameters.MinimumVerticalDragDistance;
return (horizontalMovement | verticalMovement);
}
在PreviewMouseMove
事件中调用...这是一个简化示例:
private void DragSourcePreviewMouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown && IsDragConfirmed(e.GetPosition(sender as ListBox)))
{
// Start your drag operation here
}
}
答案 2 :(得分:0)
我得到了与下面答案相同的问题和相同的来源。 好吧,如果鼠标位置不在listview项目上,我决定阻止拖动(rect 0,0,lv.width - scroll.width,lv.height - scroll.height) 所以我有一个&#34; allowdrag&#34; flag,当PreviewMouseLeftButtonDown过去时,如果鼠标指针超过必要的矩形,则变为True。
VB.NET
Private allowdrag As Boolean
Private Sub lv_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles lvAttributesDefault.PreviewMouseLeftButtonDown
Dim listView As ListView = TryCast(sender, ListView)
allowdrag = e.GetPosition(sender).X < listView.ActualWidth - SystemParameters.VerticalScrollBarWidth And e.GetPosition(sender).Y < listView.ActualHeight - SystemParameters.HorizontalScrollBarHeight
End Sub
Private Sub lv_MouseMove(sender As Object, e As MouseEventArgs) Handles lvAttributesDefault.MouseMove
Dim listView As ListView = TryCast(sender, ListView)
If e.LeftButton = MouseButtonState.Pressed And listView.SelectedItem IsNot Nothing And allowdrag Then
Dim obj As clsAttribute = CType(listView.SelectedItem, clsAttribute)
Dim dragData As New DataObject("clsAttribute", obj)
DragDrop.DoDragDrop(listView, dragData, DragDropEffects.Copy)
End If
End Sub
C#(telerik代码转换器)
private bool allowdrag;
private void lv_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ListView listView = sender as ListView;
allowdrag = e.GetPosition(sender).X < listView.ActualWidth - SystemParameters.VerticalScrollBarWidth & e.GetPosition(sender).Y < listView.ActualHeight - SystemParameters.HorizontalScrollBarHeight;
}
private void lv_MouseMove(object sender, MouseEventArgs e)
{
ListView listView = sender as ListView;
if (e.LeftButton == MouseButtonState.Pressed & listView.SelectedItem != null & allowdrag) {
clsAttribute obj = (clsAttribute)listView.SelectedItem;
DataObject dragData = new DataObject("clsAttribute", obj);
DragDrop.DoDragDrop(listView, dragData, DragDropEffects.Copy);
}
}
PS。 clsAttribute是我的自定义类。该 listview绑定到ObservableCollection(Of clsAttribute)