2010-12-23 6 views
4

Ich versuche Drag-and-Drop-Funktionalität mit MVVM schreiben, die mir PersonModel Objekte von einem ListView zu einem anderen ziehen können.WPF Drag & Drop - Holen Sie sich Original-Source-Informationen von DragEventArgs

Dies funktioniert fast, aber ich muss in der Lage sein, die ItemsSource der Quelle ListView von den DragEventArgs zu bekommen, die ich nicht herausfinden kann, wie es geht.

private void OnHandleDrop(DragEventArgs e) 
{ 
    if (e.Data != null && e.Data.GetDataPresent("myFormat")) 
    { 
     var person = e.Data.GetData("myFormat") as PersonModel; 
     //Gets the ItemsSource of the source ListView 
     .. 

     //Gets the ItemsSource of the target ListView and Adds the person to it 
     ((ObservableCollection<PersonModel>)(((ListView)e.Source).ItemsSource)).Add(person); 
    } 
} 

Jede Hilfe würde sehr geschätzt werden.

Danke!

+0

In meinem Drag & Drop-Implementierung I Klasse erstellt habe DragManager (das ist Singleton) und hinzugefügt ein privates Feld DraggingElement. Weil nur ein Element zur Zeit gezogen werden kann. – vorrtex

Antwort

4

fand ich die Antwort in another question

Die Art und Weise, es zu tun ist, um die Quelle Listview in die DragDrow.DoDragDrop Methode also passieren.

Bei dem Verfahren, das die Preview für das Listview behandelt do-

private static void List_MouseMove(MouseEventArgs e) 
{ 
    if (e.LeftButton == MouseButtonState.Pressed) 
    { 
     if (e.Source != null) 
     { 
      DragDrop.DoDragDrop((ListView)e.Source, (ListView)e.Source, DragDropEffects.Move); 
     } 
    } 
} 

und dann in der OnHandleDrop Methode den Code ändern zu

private static void OnHandleDrop(DragEventArgs e) 
{ 
    if (e.Data != null && e.Data.GetDataPresent("System.Windows.Controls.ListView")) 
    { 
     //var person = e.Data.GetData("myFormat") as PersonModel; 
     //Gets the ItemsSource of the source ListView and removes the person 
     var source = e.Data.GetData("System.Windows.Controls.ListView") as ListView; 
     if (source != null) 
     { 
      var person = source.SelectedItem as PersonModel; 
      ((ObservableCollection<PersonModel>)source.ItemsSource).Remove(person); 

      //Gets the ItemsSource of the target ListView 
      ((ObservableCollection<PersonModel>)(((ListView)e.Source).ItemsSource)).Add(person); 
     } 
    } 
}