2010-02-23 77 views
3

我目前正在將窗體應用程序移植到wpf。有一個包含文件名的列表框。應該可以將(多個)項目拖到Windows資源管理器中。 這很容易與舊的Windows窗體,但我無法找到一種方式如何可以在wpf中完成。列表框拖動在wpf

這是我使用Windows窗體使用的代碼:

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

回答

0

確保您選中(複選框)多選選項

+1

多重選擇是沒有問題的,代碼obove顯示我的方式我已經做了拖放在WIN窗體中,我想知道這是如何與WPF工作 – hans 2010-02-23 11:37:04

2

好吧...我發現基於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); 
     } 
    } 
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); 
      } 
     } 
    }