【问题标题】:listbox dragdrop in wpfwpf中的列表框拖放
【发布时间】:2010-02-23 10:57:41
【问题描述】:

我目前正在将一个 Windows 窗体应用程序移植到 wpf。有一个包含文件名的列表框。应该可以将(多个)项目拖到 Windows 资源管理器中。 这在旧的 windows 窗体中很容易,但我找不到如何在 wpf 中完成此操作。

这是我在 windows 窗体中使用的代码:

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

【问题讨论】:

    标签: wpf listview drag-and-drop


    【解决方案1】:

    好的...我找到了解决问题的方法,基于this tutorial

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

    【讨论】:

      【解决方案2】:

      尝试查看 Bea Stollnitz 的博客,了解 WPF 中拖放的彻底实现。

      Bea Stollnitz - How can I drag and drop items between data bound ItemsControls?

      【讨论】:

        【解决方案3】:
        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);
                    }
                }
            }
        

        【讨论】:

          【解决方案4】:

          确保您选中(复选框)多项选择选项

          【讨论】:

          • 多选不是问题,上面的代码显示了我在WIN Forms中完成拖放的方式,我想知道这如何与WPF一起工作
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-11
          • 1970-01-01
          • 2011-12-01
          • 2013-04-07
          • 2011-03-21
          相关资源
          最近更新 更多