gpt4 book ai didi

c# - 滚动时防止拖放

转载 作者:太空宇宙 更新时间:2023-11-03 16:06:00 27 4
gpt4 key购买 nike

我正在处理一个 Wpf 项目,但现在我遇到了 ListView 问题。

原来我在 ListView 上实现了一个Drag&Drop 功能,效果很好。当我尝试向下或向上滚动时出现问题。通过这样做,拖放 功能被激活,阻止我继续滚动。

我找到了 this solution这表明我们需要将控件附加到 ScrollChanged 事件。

<ListView ScrollViewer.ScrollChanged="listView1_ScrollChanged"...

但我真的不知道在该处理程序中该做什么。 如何禁用该事件的拖放功能?我怎样才能再次启用它?或者,有没有更好的方法来解决这个问题??

这是我的拖放代码:

 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);
}
}

提前致谢。

最佳答案

如果您不在 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;
}

这应该可行,我是在浏览器中编写的,所以如果我做错了什么请原谅,关于格式,我希望你明白了。

关于c# - 滚动时防止拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com