简体   繁体   English

如何通过拖放将列表视图项从一个列表移动到另一个列表? UWP C#

[英]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.DataViewDataPackage中检索存储的项目值。

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.

相关问题 当通过单击按钮创建列表视图时,如何通过拖放在列表视图之间移动列表视图项? UWP C# - How to move List View Item between List Views with drag and drop when the List Views are created with a click of a button? UWP C# 将项目从一个List &lt;&gt;移到另一个。 C# - Move item form one List<> to another. C# 如何从一个坐标拖放到另一C# - How to drag and drop from one coordinate to another C# 如何在C#中将所选项目从一个C1List移动​​到另一个? - How to move move selected items from one C1List to another in C#? C#LINQ如何从另一个列表中的一个列表中找到匹配项,并在另一个列表中检查属性 - C# LINQ How to find matching item from one list in another list and check for the property in the other list 如何在C#中将所选项目从一个列表视图移动到另一个列表视图? - How to move selected item from one listview to another in C#? C#从另一个表单将项目添加到列表视图 - C# Add Item to List View from another form 如何将列表项拖放到标签上,使其成为C#中标签的文本? - How can you drag and drop a list item to a label so it's the label's text in C#? c# 将列表项的索引从一种形式传递到另一种形式 - c# Passing an index of a list item from one form to another 将一个项目的值从列表复制到 c# 中的另一个 - Copy value of one item from a list into another in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM