title: Drag and Drop ListBoxItems generically
body: <p>I want to implement a drag and drop on multiple listboxes that are data bound - using the MVVM pattern.  I am not trying to drag and drop between listboxes but rather want the user to be able to drag/drop listboxitems in each listbox so they can rearrange the sort order.  I found this post on SO which was VERY helpful:</p>
  
  <p><a href="https://stackoverflow.com/questions/3350187/wpf-c-rearrange-items-in-listbox-via-drag-and-drop">WPF C#: Rearrange items in listbox via drag and drop</a></p>
  
  <p>I wanted to try and make the methods more "generic" so that it would work on any listbox that is binding to different types of Observable Collections.  So say this is my XAML in the VIEW:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"&gt;
  
      &lt;Window.Resources&gt;
          &lt;Style TargetType="{x:Type ListBoxItem}" x:Key="ListBoxItemDragDrop"&gt;
              &lt;Setter Property="AllowDrop" Value="True" /&gt;
              &lt;EventSetter Event="PreviewMouseMove" Handler="ListBoxItem_PreviewMouseMoveEvent" /&gt;
              &lt;EventSetter Event="Drop" Handler="listbox1_Drop" /&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition /&gt;
              &lt;ColumnDefinition /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;ListBox Name="listbox1"
                   ItemsSource="{Binding OCofType1}"
                   ItemContainerStyle="{StaticResource ListBoxItemDragDrop}" /&gt;
  
          &lt;ListBox Name="listbox2" Grid.Column="1"
                   ItemsSource="{Binding OCofType2}"
                   ItemContainerStyle="{StaticResource ListBoxItemDragDrop}"/&gt;
      &lt;/Grid&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <p>Where the OC bindings are ObservalbeCollection&lt;Type1&gt; and ObservalbeCollection&lt;Type2&gt;.  Here is the method in the VIEW that grabs the mouse move event and checks if it is a drag:</p>
  
  <pre><code>void ListBoxItem_PreviewMouseMoveEvent(object sender, MouseEventArgs e)
  {
      if (e.LeftButton == MouseButtonState.Pressed &amp;&amp; sender is ListBoxItem)
      {
          ListBoxItem draggedItem = (ListBoxItem)sender;
          DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
          draggedItem.IsSelected = true;
      }
  }
  </code></pre>
  
  <p>This seems "generic" enough.  Next would be the method, also in the VIEW, the handles the drop and this is where I get stuck:</p>
  
  <pre><code>void ListBoxItem_Drop(object sender, DragEventArgs e)
  {
      object Target = ((ListBoxItem)(sender)).DataContext;
      object Dropped = e.Data.GetData(Target.GetType());
  
      int RemoveIndex = listbox1.Items.IndexOf(Dropped);
      int TargetIndex = listbox1.Items.IndexOf(Target);
  
      ListBox container = ((DependencyObject)sender).GetAncestor&lt;ListBox&gt;();
  
      if (RemoveIndex &lt; TargetIndex)
      {
          //THESE WILL NOT WORK IF I AM DOING BINDINGS THROUGH THE ITEMSSOURCE
          //container.Items.Insert(RemoveIndex + 1, Dropped);
          //container.Items.RemoveAt(RemoveIndex);
  
          //SO HAVE TO USE THE ITEMSSOURCE DIRECTLY BUT HOW WITHOUT A SPECIFIC CAST TO THE OC&lt;TYPE#&gt;
          container.ItemsSource.Insert(RemoveIndex + 1, Dropped);  //ERROR: IENUMERATOR DOES NOT CONTAIN A DEFINITION FOR INSERT....
          container.ItemsSource.RemoveAt(RemoveIndex); //ERROR: IENUMERATOR DOES NOT CONTAIN A DEFINITION FOR REMOVEAT....
      }
      else
          if (container.Items.Count &gt; RemoveIndex)
          {
              //THESE WILL NOT WORK IF I AM DOING BINDINGS THROUGH THE ITEMSSOURCE                    
              //container.Items.Insert(TargetIndex, Dropped);
              //container.Items.RemoveAt(RemoveIndex + 1); 
  
              //SO HAVE TO USE THE ITEMSSOURCE DIRECTLY BUT HOW WITHOUT A SPECIFIC CAST TO THE OC&lt;TYPE#&gt;
              container.ItemsSource.Insert(TargetIndex, Dropped);  //ERROR: IENUMERATOR DOES NOT CONTAIN A DEFINITION FOR INSERT....
              container.ItemsSource.RemoveAt(RemoveIndex + 1);  //ERROR: IENUMERATOR DOES NOT CONTAIN A DEFINITION FOR REMOVEAT....
  
          }
  }
  </code></pre>
  
  <p>That find ancestor functions is this (from another post on SO):</p>
  
  <pre><code>static T FindAnchestor&lt;T&gt;(DependencyObject current) where T : DependencyObject
  {
      do
      {
          if (current is T)
              return (T)current;
  
          current = VisualTreeHelper.GetParent(current);
      }
      while (current != null);
      return null;
  }
  </code></pre>
  
  <p>In the drop function, this might work if I was directly adding to the ListBox's List collections.  But since I am doing bindings to collections in the VIEWMODEL, it will error saying that I have to work with those objects though the ItemsSource.  But if I use the ItemsSource I would have to make mutible versions of the function for each OC Type since I would not what to cast the ItemsSource too at run time.  I could keep it down to 1 function using an if statement which determine what to cast it to explicitly but it would be alot cleaner not having to remember to update that if for each new OC it is applied to.</p>
  
  <p>The question is then how do I add/move items to the ItemsSource without knowing <i>exactly</i> what to cast too?</p>
  
  <p>Thanks for any help.</p>