2012-07-26 61 views
-1

我有一個WPF-Datagrid,我可以放置一個元素。這是一個Textelement.txt文件(例如用記事本++打開)中刪除。是否有可能在我的Drop事件中獲取有關.txt文件的信息?獲取拖放的SOURCE

編輯:

void OnDragDrop(object sender, DragEventArgs e) 
{ 
    String text = e.Data.GetData(DataFormats.Text, true); 
} 

在這裏,我可以讓我的一滴元素的文本,但我發現沒有辦法獲取源文件,這是拖動上手。

+0

[你有什麼嘗試?](http://whathaveyoutried.com)顯示一些研究工作! – GameScripting 2012-07-26 14:47:59

+0

我沒有直接嘗試過,因爲我不知道解決它。DragEventArgs只提供有關我的丟失控件的信息,而不是數據的來源。 – Kooki 2012-07-26 15:14:30

+0

看看文檔吧http://msdn.microsoft.com/en-us/library/ms742859.aspx – GameScripting 2012-07-26 15:21:52

回答

0

確定在這裏你去:

你必須做三兩件事,使在WPF drag'n下降:

  1. 告訴元素來支持drag'n下降
  2. 設置一個DragOver事件
  3. 設置一個Drop事件

OK,讓我們先來看看XAML:

<DataGrid AllowDrop="True" 
      DragOver="DataGrid_DragOver" 
      Drop="DataGrid_Drop"/> 

且事件處理代碼:

private void DataGrid_DragOver(object sender, DragEventArgs e) 
{ 
    // check if the element dragged over is one or more files 
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
    { 
     // if so, show a link cursor 
     e.Effects = DragDropEffects.Link; 
    } 
    else 
    { 
     // otherwise show a "block" cursor 
     e.Effects = DragDropEffects.None; 
    } 

    // IMPORTANT: mark the event as "handled by us", to apply the drag effects 
    e.Handled = true; 
} 

private void DataGrid_Drop(object sender, DragEventArgs e) 
{ 
    // Check if the data dropped is one or more files 
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
    { 
     // get the file pathes from the data object 
     string[] filePaths = (e.Data.GetData(DataFormats.FileDrop) as string[]); 

     // do something with the pathes 
     /* ... */ 
    } 
} 

欲瞭解更多信息,請參閱MSDN documentation

+0

謝謝4幫助,但這不是我的問題。我知道如何刪除文件等,但我只會從文件中刪除文本,所以沒有DataFormats.FileDrop,只有DataFormat.Text.No我想要找到文件,我從中取出文本並將其放入我的應用程序 – Kooki 2012-07-27 13:43:11

+0

如果您從Notepad ++等應用程序中拖放文本,這是不可能的,因爲應用程序(Notepad ++)僅將實際數據包含在拖放操作中。你可以通過調用'e.Data.GetFormats()'來獲得包含在動作中的所有信息,然後使用'e.Data.GetData(currentDataFormat)'獲取當前格式的信息。 – GameScripting 2012-07-27 13:53:06

+0

非常感謝您的幫助。如果有人知道如何解決,請寫下;-) – Kooki 2012-07-29 10:42:35

0

思考掉落相同的方式 剪切&粘貼將&

- 通常只有「數據」被活動期間拖過,以及有關它的源沒有額外的元數據。

對此的一個例外是從網頁中拖動文本時。 DataFormats.Html將包含文本來自的SourceURL。