title: wpf Drag and Drop at exact mouse location
body: <p>I have a ListBox and a Canvas. I want to Drag a ListBoxItem onto a Canvas exactly where the mouse is placed.This is my xaml. Problem is the items are placed one on top of other irrespective of the mouse position. How can I place the dragged items at the exact location based on the mouse pointer on the canvas. Please help.</p>
  
  <pre><code>&lt;DockPanel LastChildFill="True" &gt;
          &lt;ListBox DockPanel.Dock="Left" Name="lstLabels"&gt;
              &lt;ListBox.Resources&gt;
                  &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
                      &lt;EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBoxItem_PreviewMouseLeftButtonDown"/&gt;
                      &lt;EventSetter Event="PreviewMouseMove" Handler="ListBoxItem_PreviewMouseMove"/&gt;
  
                  &lt;/Style&gt;
              &lt;/ListBox.Resources&gt;
              &lt;ListBoxItem&gt;A&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;B&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;C&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;D&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;E&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;F&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;G&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;H&lt;/ListBoxItem&gt;
          &lt;/ListBox&gt;
          &lt;Canvas AllowDrop="True" Background="Azure"
                  DragEnter="cvsSurface_DragEnter" Drop="cvsSurface_Drop" 
                  Name="cvsSurface" &gt;
          &lt;/Canvas&gt;
      &lt;/DockPanel&gt;
  </code></pre>
  
  <p>This is my complete cs code</p>
  
  <pre><code> private ListBoxItem draggedItem;
   private Point startDragPoint;
  private void cvsSurface_DragEnter(object sender, DragEventArgs e)
          {
              if (e.Data.GetDataPresent(DataFormats.Text))
              {
                  e.Effects = DragDropEffects.Copy;
              }
              else
              {
                  e.Effects = DragDropEffects.None;
              }
          }
          private void cvsSurface_Drop(object sender, DragEventArgs e)
          {
              Label newLabel = new Label();
              newLabel.Content = e.Data.GetData(DataFormats.Text);
  
              cvsSurface.Children.Add(newLabel);
              Canvas.SetLeft(newLabel, 100);
              Canvas.SetTop(newLabel, 200);
  
              draggedItem = null;
              lstLabels.SelectedItem = null;
          }
          private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {
              draggedItem = sender as ListBoxItem;
              startDragPoint = e.GetPosition(null);
          }
          private void ListBoxItem_PreviewMouseMove(object sender, MouseEventArgs e)
          {
              Point position = e.GetPosition(null);
              if (draggedItem != null)
              {
                  DragDrop.DoDragDrop(draggedItem, draggedItem.Content, DragDropEffects.Copy);
              }
          }
  </code></pre>