2015-03-31 105 views
1

所以我有一個ListView其中包含的MyFilesMyFolders 列表這兩個類實現我的接口IExplorerItem如何拖放項目到ListView和檢測項目就滴到

現在,我已經建立了我列表視圖,這樣我可以拖放到它像這樣:

<ListView ItemsSource="{Binding DisplayedItems}" AllowDrop="True"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Drop"> 
      <Command:EventToCommand Command="{Binding DropFiles}" PassEventArgsToCommand="True"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</ListView> 

和命令是:

private RelayCommand<DragEventArgs> _dropFiles; 

/// <summary> 
/// Gets the DropFiles. 
/// </summary> 
public RelayCommand<DragEventArgs> DropFiles 
{ 
    get 
    { 
     return _dropFiles 
      ?? (_dropFiles = new RelayCommand<DragEventArgs>(
      args => 
      { 
       if (args.Data.GetDataPresent(DataFormats.FileDrop)) 
       { 
        // Note that you can have more than one file. 
        string[] files = (string[])args.Data.GetData(DataFormats.FileDrop); 
        //do my thing with my files 
       } 
      } 
    } 
} 

所以這適用於拖放文件和處理它們。 但我不想檢測文件被丟棄的項目。

例如如果IExplorerItem它被丟棄是一個MyFolder對象,然後將它們添加到該文件夾​​。 這可能嗎?

回答

0

明白了,所以在我的RelayCommand我只需要深入瞭解DragEventArgs類。

private RelayCommand<DragEventArgs> _dropFiles; 

/// <summary> 
/// Gets the DropFiles. 
/// </summary> 
public RelayCommand<DragEventArgs> DropFiles 
{ 
    get 
    { 
     return _dropFiles 
      ?? (_dropFiles = new RelayCommand<DragEventArgs>(
      args => 
      { 
       if (args.Data.GetDataPresent(DataFormats.FileDrop)) 
       { 
        // Note that you can have more than one file. 
        string[] files = (string[])args.Data.GetData(DataFormats.FileDrop); 
        if ((args.OriginalSource as FrameworkElement).DataContext is MyFolder) 
        { 
         //put the files in a folder 
        } 
        else 
        { 
         //do something else 
        } 
       } 
      } 
    } 
}