title: Two Reordering ListBoxes on Same page, prevent user from dragging and dropping between
body: <p>I have a WPF c# application that has a window with two Reordering Listboxes right next to each other. I used the examples in <a href="https://stackoverflow.com/questions/3350187/wpf-c-rearrange-items-in-listbox-via-drag-and-drop">this link</a> to make the listbox user control. Unfortunately this allows the user to drag from one box and drop in the other. How can I make sure that this doesn't happen?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    public void setItems(List&lt;string&gt; values){
          _items = new ObservableCollection&lt;Item&gt;();
          foreach (string s in values)
          {
              _items.Add(new Item(s));
          }
  
          listBox.DisplayMemberPath = "Name";
          listBox.ItemsSource = _items;
  
          listBox.PreviewMouseMove += ListBox_PreviewMouseMove;
  
          var style = new Style(typeof(ListBoxItem));
          style.Setters.Add(new Setter(ListBoxItem.AllowDropProperty, true));
          style.Setters.Add(
              new EventSetter(
                  ListBoxItem.PreviewMouseLeftButtonDownEvent,
                  new MouseButtonEventHandler(ListBoxItem_PreviewMouseLeftButtonDown)));
          style.Setters.Add(
                  new EventSetter(
                      ListBoxItem.DropEvent,
                      new DragEventHandler(ListBoxItem_Drop)));
          listBox.ItemContainerStyle = style;
      }
  
  
      private void ListBox_PreviewMouseMove(object sender, MouseEventArgs e)
      {
  
              Point point = e.GetPosition(null);
              Vector diff = _dragStartPoint - point;
              if (e.LeftButton == MouseButtonState.Pressed &amp;&amp;
                  (Math.Abs(diff.X) &gt; SystemParameters.MinimumHorizontalDragDistance ||
                      Math.Abs(diff.Y) &gt; SystemParameters.MinimumVerticalDragDistance))
              {
                  var lb = sender as ListBox;
                  var lbi = FindVisualParent&lt;ListBoxItem&gt;(((DependencyObject)e.OriginalSource));
                  if (lbi != null)
                  {
                      DragDrop.DoDragDrop(lbi, lbi.DataContext, DragDropEffects.Move);
                  }
              }
  
      }
  
  
      private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          _dragStartPoint = e.GetPosition(null);
      }
  
  
      private void ListBoxItem_Drop(object sender, DragEventArgs e)
      {
          if (sender is ListBoxItem)
          {
              var source = e.Data.GetData(typeof(Item)) as Item;
              var target = ((ListBoxItem)(sender)).DataContext as Item;
  
  
                  int sourceIndex = listBox.Items.IndexOf(source);
                  int targetIndex = listBox.Items.IndexOf(target);
  
                  Debug.WriteLine("Target: " + targetIndex.ToString());
  
                  Move(source, sourceIndex, targetIndex);
  
          }
      }
  
  
      private void Move(Item source, int sourceIndex, int targetIndex)
      {
          if (sourceIndex &lt; targetIndex)
          {
              _items.Insert(targetIndex + 1, source);
              _items.RemoveAt(sourceIndex);
          }
          else
          {
              int removeIndex = sourceIndex + 1;
              if (_items.Count + 1 &gt; removeIndex)
              {
                  _items.Insert(targetIndex, source);
                  _items.RemoveAt(removeIndex);
              }
          }
      }
  }
  </code></pre>