WPF中的列表框拖放

3

我目前正在将一个Windows Forms应用程序转换为WPF。有一个包含文件名的列表框。应该可以将(多个)项目拖到Windows资源管理器中。

在旧的Windows Form中,这很容易实现,但我无法找到在WPF中如何实现此功能的方法。

这是我在Windows Forms中使用的代码:

void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
    string[] files = GetSelection();
    if (files != null)
    {
        DoDragDrop(new DataObject(DataFormats.FileDrop, files), DragDropEffects.Copy );  
    }
}
4个回答

2

好的...我找到了一个解决方案,基于这个教程

    private void List_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        // Store the mouse position
        startPoint = e.GetPosition(null);
    }

    private void List_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)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }

            string[] files = GetSelection();
            string dataFormat = DataFormats.FileDrop;
            DataObject dataObject = new DataObject(dataFormat, files);
            DragDrop.DoDragDrop(listView1, dataObject, DragDropEffects.Copy);
        }
    }

1
try this code that help u to solve your problem



public partial class MainWindow : Window
    {
        List<string> file = new List<string>();
        public MainWindow()
        {
            InitializeComponent();
            file.Add(@"D:\file1.txt");
            file.Add(@"D:\folder1");
            file.Add(@"D:\folder2");
            file.Add(@"D:\folder3");
            lstTest.DataContext = file;
        }


        private Point start;
        private void lstTest_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.start = e.GetPosition(null);
        }

        private void lstTest_MouseMove(object sender, MouseEventArgs e)
        {
            Point mpos = e.GetPosition(null);
            Vector diff = this.start - mpos;
            if (e.LeftButton == MouseButtonState.Pressed && Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance && Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
            {
                if (this.lstTest.SelectedItems.Count == 0)
                {
                    return;
                }
                string[] Files = new string[file.Count] ;
                for (int i = 0; i < file.Count; i++)
                {
                    Files[i] = file[i];
                }
                string dataFormat = DataFormats.FileDrop;
                DataObject dataObject = new DataObject(dataFormat, (lstTest.SelectedItems.Cast<string>()).ToArray<string>());
                DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);
            }
        }
    }

1

0

请确保您已勾选多选选项框(checkbox)


1
多选不是问题,上面的代码展示了我在WIN Forms中实现拖放的方式,我想知道如何在WPF中实现。 - hans

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接