[英]How to move List View Item from one list to another with drag and drop? UWP C#
I have many List Views in an UWP application and I would want to be able to move one item from one List View to another List View我在 UWP 应用程序中有许多列表视图,我希望能够将一个项目从一个列表视图移动到另一个列表视图
I know about the AllowDrop or CanDragItems properties and that you need to handle some events for drag and drop to work, although I just don't know how to do it.我知道 AllowDrop 或 CanDragItems 属性,并且您需要处理一些事件才能使拖放工作,尽管我只是不知道该怎么做。
If you want to add ListView controls by clicking Add button and move items between ListView controls, please check the following code as a sample.如果您想通过单击添加按钮添加 ListView 控件并在 ListView 控件之间移动项目,请检查以下代码作为示例。 The following code is different from the offical sample , it use ObservableCollection
to complete the drag and drop operation.以下代码与官方示例不同,它使用ObservableCollection
来完成拖放操作。 You could drag an item from source ListView
item to target ListView
, and also drag an item from original target ListView
to original source ListView
.您可以将项目从源ListView
项目拖动到目标ListView
,也可以将项目从原始目标ListView
拖动到原始源ListView
。
You could click the Add
button twice and then two ListView
with two items are added.您可以单击“ Add
”按钮两次,然后添加两个带有两个项目的ListView
。 You can drag any item from any ListView
control to another.您可以将任何项目从任何ListView
控件拖动到另一个。 If you want to keep the dragged item in the source ListView
control, just comment the code dragCollection.Remove(dragedItem as string);
如果要在源ListView
控件中保留被拖动的项,只需注释代码dragCollection.Remove(dragedItem as string);
. .
For example:例如:
private ObservableCollection<string> dragCollection;
private ObservableCollection<string> dropCollection;
private object dragedItem;
private ListView dragListView;
private ListView dropListView;
……
private void AddButton_Click(object sender, RoutedEventArgs e)
{
ListView listView = new ListView();
listView.CanDragItems = true;
listView.CanDrag = true;
listView.AllowDrop = true;
listView.ReorderMode = ListViewReorderMode.Enabled;
listView.CanReorderItems = true;
listView.ItemsSource = new ObservableCollection<string>() { "item1","item2" };
listView.DragItemsStarting += ListView_DragItemsStarting;
//listView.DropCompleted += ListView_DropCompleted;
listView.DragEnter += ListView_DragEnter;
listView.Drop += ListView_Drop;
listView.DragOver += ListView_DragOver;
listView.BorderBrush = new SolidColorBrush(Colors.Red);
listView.BorderThickness = new Thickness(1);
stackPanel.Children.Add(listView);
}
private void ListView_DragOver(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Move;
}
private void ListView_Drop(object sender, DragEventArgs e)
{
dropListView = sender as ListView;
if(dropListView!=null)
{
dropCollection = dropListView.ItemsSource as ObservableCollection<string>;
if (dragedItem != null)
{
dropCollection.Add(dragedItem as string);
//If you need to delete the draged item in the source ListView, then use the following code
dragCollection.Remove(dragedItem as string);
dragedItem = null;
}
}
}
private void ListView_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = (e.DataView.Contains(StandardDataFormats.Text) ? DataPackageOperation.Move : DataPackageOperation.None);
}
private void ListView_DropCompleted(UIElement sender, DropCompletedEventArgs args)
{
var listView = sender as ListView;
if (listView != null)
{
dropListView = listView;
dropCollection = listView.ItemsSource as ObservableCollection<string>;
if(dropListView==dragListView)
{
return;
}
}
}
private void ListView_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{
var listView = sender as ListView;
if(listView!=null)
{
dragListView = listView;
dragCollection = listView.ItemsSource as ObservableCollection<string>;
if (dropListView == dragListView)
{
return;
}
if(e.Items.Count==1)
{
dragedItem = e.Items[0];
e.Data.RequestedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
}
}
}
For more information about dragging and dropping, you could refer to the document( https://docs.microsoft.com/en-us/windows/uwp/design/input/drag-and-drop ).有关拖放的更多信息,您可以参考文档( https://docs.microsoft.com/en-us/windows/uwp/design/input/drag-and-drop )。
Any concerns about the code, please feel free to contact me.对代码有任何疑问,请随时与我联系。
To implement dragging, you must set CanDragItems
on the source ListView
and AllowDrop
on the target ListView
.要实现拖动,您必须在源ListView
上设置CanDragItems
并在目标ListView
上设置AllowDrop
。 Then, you must handle DragItemsStarting
event on the source list.然后,您必须处理源列表上的DragItemsStarting
事件。 Within this handler you can store the dragged data inside the DragItemsStartingEventArgs.Data
property.在此处理程序中,您可以将拖动的数据存储在DragItemsStartingEventArgs.Data
属性中。 Afterwards, you handle Drop
event on the target list and retrieve the stored item values from the DataPackage
using DragEventArgs.DataView
.之后,您处理目标列表上的Drop
事件并使用DragEventArgs.DataView
从DataPackage
中检索存储的项目值。
To see all the moving parts of this in action, I recommend the official UWP samples for drag & drop which are available on GitHub .要查看此操作的所有活动部分,我推荐官方 UWP 拖放样本,可在 GitHub 上获得。 The first scenario of this sample show dragging items from and to a ListView
including reordering support.此示例的第一个场景显示将项目从 ListView 拖放到ListView
,包括重新排序支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.