title: How can I drag a button that's on a canvas in WPF
body: <p>I spent the last few days trying to figure how I could use the DragDrop feature in C#'s WPF so I could create a button and drag it around in canvas. I think I am missing someting but after an exhasting effort researching I can't figure out what it is. All I need to do is drag the button around in canvas. </p>
  
  <p>Here is my XAML code:</p>
  
  <pre><code>x:Class="DragDrop_Test.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:DragDrop_Test"
          mc:Ignorable="d"
          Title="MainWindow" Height="350" Width="525"&gt;
  
      &lt;Canvas Name="MyCanvas" Width = "300" Height= "200" Background="WhiteSmoke" Margin= "10, 0,190,100" AllowDrop="True" Drop="Canvas_Drop"&gt;
          &lt;Button Name="btn" Content="Button1" MouseMove="btn_MouseMove" PreviewMouseLeftButtonDown="btn_PreviewMouseLeftButtonDown"&gt;
  
          &lt;/Button&gt;
      &lt;/Canvas&gt;
  
  
  &lt;/Window&gt;
  </code></pre>
  
  <p>And here is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace DragDrop_Test
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          Control draggedItem;
          bool IsDragging;
          public MainWindow()
          {
              InitializeComponent();
              IsDragging = false;
          }
  
  
          private void Canvas_Drop(object sender, DragEventArgs e)
          {
              if (!IsDragging)
                  return;
  
              IsDragging = false;
          }
  
  
  
          private void btn_MouseMove(object sender, MouseEventArgs e)
          {
              var dependencyObject = (Button)sender;
              draggedItem = dependencyObject;
  
              if (!IsDragging) 
                  return;
              if (dependencyObject != null &amp;&amp; dependencyObject.IsMouseOver)
              {
                  DragDrop.DoDragDrop(dependencyObject, dependencyObject, DragDropEffects.Move);
              }
          }
  
          private void btn_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {
              IsDragging = true;
              draggedItem = (Button)sender;
          }
      }
  }
  </code></pre>
  
  <p>Thanks in advance </p>