如何将列表视图中的所有选定项目拖动到C#中的另一个列表视图 [英] How to drag all the selected items from a listview to another listview in C#
本文介绍了如何将列表视图中的所有选定项目拖动到C#中的另一个列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ListView sourcelistview = null;
ListView targetlistview = null;
Point startPoint;
public Window1()
{
InitializeComponent();
lbOne.Items.Add("xxxx");
lbOne.Items.Add("yyyy");
lbOne.Items.Add("zzzz");
lbOne.Items.Add("uuuu");
lbOne.Items.Add("vvvv");
lbOne.Items.Add("wwww");
lbOne.Items.Add("rrrr");
lbOne.Items.Add("ssss");
}
private void lbOne_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(null);
}
private void lbTwo_Drop(object sender, DragEventArgs e)
{
targetlistview = (ListView)sender;
var Sourcedata = sourcelistview.SelectedItems.Cast<object>().ToArray();
foreach (string item in Sourcedata)
{
targetlistview.Items.Add(item);
sourcelistview.Items.Remove(item);
}
}
private void lbOne_MouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed && Math.Abs(diff.X) > SystemParameters.MinimumVerticalDragDistance)
{
{
sourcelistview = (ListView)sender;
DragDrop.DoDragDrop(sourcelistview, sourcelistview.SelectedItems.ToString(), DragDropEffects.Copy);
}
}
}
这适用于单个事件拖动,但当我尝试拖动多个事件时,我选择的最后一个项目被取消选择,其他项目被移动。任何解决方案?
This works for single event drag, but when I attempt to drag multiple events, the last item I select gets deselected and other items are moved. Any solution for this?
推荐答案
CodeProject(在这种情况下,Josh Smith,WPF大师)是你的朋友:在WPF ListView中拖放项目[ ^ ]。
而且,Nick Polyak可以帮助你进行多选拖放:WPF中的MultiSelect拖放[ ^ ]。
CodeProject (in this case Josh Smith, WPF guru) is your friend: "Drag and Drop Items in a WPF ListView" [^].
And, Nick Polyak is here to help you with multi-select drag/drop: "MultiSelect Drag and Drop in WPF" [^].
这篇关于如何将列表视图中的所有选定项目拖动到C#中的另一个列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文