2012-11-09 39 views
2

我做了一些研究,發現這一點:拖動從WPF下降到記事本

DataObject d = new DataObject(); 
d.SetData(DataFormats.Serializable, myObject); 
d.SetData(DataFormats.Text, myObject.ToString()); 
myForm.DoDragDrop(d, DragDropEffects.Copy); 

代碼片段拖在Win形式下降。

,我試圖實現它像這樣(WPF):

private void listView1_MouseMove(object sender, MouseEventArgs e) 
    { 
     try 
     { 
      if (e.LeftButton == MouseButtonState.Pressed) 
      { 
       DataObject d = new DataObject(); 
       d.SetData(DataFormats.Serializable, listView1.SelectedItem); 
       d.SetData(DataFormats.Text, listView1.SelectedItem.ToString()); 
       DragDrop.DoDragDrop(listView1, d, DragDropEffects.Copy); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

    } 

現在我想,當我拖到扔下的ListViewItem到記事本中,它可能會複製所選項目的類名(因爲這是什麼listView1.SelectedItem.ToString())是...但是,而記事本顯示一個懸停時取消符號的圖片,當我放開鼠標按鈕時沒有複製任何東西。

總體目標是將類更改爲以逗號分隔的字符串,以便在將paste複製到記事本中時,該類的所有數據都將以不錯的格式顯示。

但是,如果有人可以幫助我剛剛得到的類名來複制我敢肯定,我可以從那裏找到答案:○

+1

我想嘗試刪除第一個setdata調用。 – weston

+0

是的,我想這有點多餘哈哈,仍然無法正常工作 –

+2

你確定記事本允許刪除文本項嗎?我看到我可以將一個文件放入記事本中,並將其打開並讀入,但似乎無法刪除文本。 –

回答

3

所以....是啊。

bool alreadycopying = false; 

    private void listView1_MouseMove(object sender, MouseEventArgs e) 
    { 
     try 
     { 
      if (e.LeftButton == MouseButtonState.Released) 
      { 
       alreadycopying = false; 
      } 


      if (e.LeftButton == MouseButtonState.Pressed && alreadycopying == false) 
      { 
       alreadycopying = true; 
       System.IO.StreamWriter test = new System.IO.StreamWriter(@"C:\SuperSecretTestFile.txt"); 
       test.WriteLine("Test"); 
       test.Close(); 

       List<String> testlist = new List<string>(); 
       testlist.Add(@"C:\SuperSecretTestFile.txt"); 

       DataObject d = new DataObject(); 
       d.SetData(DataFormats.FileDrop, testlist.ToArray<string>()); 
       DragDrop.DoDragDrop(listView1, d, DragDropEffects.All); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

    } 

經過對可憐的筆記本電腦技術的大打擊後,C#出現了勝利<。 <