title: Unable to cast object of type 'System.Windows.Controls.Grid' to type 'System.Windows.Shapes.Ellipse'?
body: <p>I'm implementing a drag and drop wpf application and i have created 3 ellipse, i'm using thumb control to drag and drop the ellipse in the map, and i wish to get the drop position for ellipse. However i get an error when drag ellipse as below:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.Windows.Controls.Grid' to type
    'System.Windows.Shapes.Ellipse'</p>
  </blockquote>
  
  <p>My XAML:</p>
  
  <pre><code> &lt;Window x:Class="DragandDropMFP.MainWindow"
                  xmlns:local="clr-namespace:DragandDropMFP"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                  Title="MainWindow" Height="350" Width="525"&gt;
  
      &lt;Grid x:Name="maingrid" MouseMove="MainGrid_MouseMove"&gt;
          &lt;Grid.Background&gt;
              &lt;ImageBrush ImageSource="/Map/Capture.Png" /&gt;
          &lt;/Grid.Background&gt;
  
  
          &lt;Canvas&gt;
              &lt;Thumb Canvas.Left="38" Canvas.Top="22" DragDelta="Thumb_DragDelta"&gt;
                  &lt;Thumb.Template&gt;
                      &lt;ControlTemplate&gt;
                          &lt;Viewbox Width="50" Height="50"&gt;
                              &lt;Grid Width="20" Height="20"&gt;
                                  &lt;Ellipse 
                                   Fill="Blue"
                                   MouseMove="DragMouseMove"/&gt;
                                  &lt;TextBlock HorizontalAlignment="Center" Text="Printer1" FontSize="4" TextAlignment="Center" VerticalAlignment="Center"/&gt;
                              &lt;/Grid&gt;
                          &lt;/Viewbox&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Thumb.Template&gt;
              &lt;/Thumb&gt;
  
  
              &lt;Thumb Canvas.Left="37" Canvas.Top="100" DragDelta="Thumb_DragDelta"&gt;
                  &lt;Thumb.Template&gt;
                      &lt;ControlTemplate&gt;
                          &lt;Viewbox Width="50" Height="50"&gt;
                              &lt;Grid Width="20" Height="20"&gt;
                                  &lt;Ellipse Fill="Yellow"
                                   MouseMove="DragMouseMove"/&gt;
                                  &lt;TextBlock HorizontalAlignment="Center" Text="Printer2" FontSize="4" TextAlignment="Center" VerticalAlignment="Center"/&gt;
                              &lt;/Grid&gt;
                          &lt;/Viewbox&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Thumb.Template&gt;
              &lt;/Thumb&gt;
  
  
              &lt;Thumb Canvas.Left="37" Canvas.Top="174" DragDelta="Thumb_DragDelta"&gt;
                  &lt;Thumb.Template&gt;
                      &lt;ControlTemplate&gt;
                          &lt;Viewbox Width="50" Height="50"&gt;
                              &lt;Grid Width="20" Height="20"&gt;
                                  &lt;Ellipse Fill="Red"
                                   MouseMove="DragMouseMove"/&gt;
                                  &lt;TextBlock HorizontalAlignment="Center" Text="Printer3" FontSize="4" TextAlignment="Center" VerticalAlignment="Center"/&gt;
                              &lt;/Grid&gt;
                          &lt;/Viewbox&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Thumb.Template&gt;
              &lt;/Thumb&gt;
          &lt;/Canvas&gt;
  
          &lt;TextBlock x:Name="ctlStatus" HorizontalAlignment="Stretch" Height="30" TextWrapping="Wrap" Text="status" VerticalAlignment="Bottom" RenderTransformOrigin="0.495,-4.7" /&gt;
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>My xaml.cs :</p>
  
  <pre><code>  public MainWindow()
          {
              InitializeComponent();
          }
  
         private void MainGrid_MouseMove(object sender, MouseEventArgs e)
          {
              Mouse.OverrideCursor = Cursors.Arrow;
              objmoveposition(sender, e);
          }
  
          private void DragMouseMove(object sender, MouseEventArgs e)
          {
  
              Mouse.OverrideCursor = Cursors.Hand;
              objmoveposition(sender, e);
          }
  
          private void objmoveposition(object sender, MouseEventArgs e)
          {
  
              if (e.LeftButton == MouseButtonState.Pressed)
              {
                  if (Mouse.OverrideCursor == Cursors.Hand)
                  {
                      Ellipse objTextbox = (Ellipse)sender; &lt;--Error
  
                  if (objTextbox != null)
                      {
                      //----&lt; Move Control &gt;---- 
                      Point mousePoint = e.GetPosition(this);
  
                      //&lt; Vertical &gt; 
                      int posY = (int)mousePoint.Y;
                      int actHeight = (int)Application.Current.MainWindow.Height;
                      int margin_Bottom = actHeight - (posY + (int)objTextbox.Height + (int)SystemParameters.CaptionHeight + (int)SystemParameters.BorderWidth + 4);
  
  
                      //&lt; Horizontal &gt; 
                      int posX = (int)mousePoint.X;
                      int actWidth = (int)Application.Current.MainWindow.Width;
                      int margin_Right = actWidth - (posX + (int)objTextbox.Width + (int)SystemParameters.BorderWidth);
  
  
                      ctlStatus.Text = "Top=" + posY + " margin_bottom=" + margin_Bottom + " WinHeigth=" + actHeight + Environment.NewLine + " Left=" + posX + " margin_Right=" + margin_Right + "WinWidth=" + actWidth;
                      //ctlStatus.Text = "position=" + objTextbox.ActualHeight;
                  }
               }
  
              }
  
  
          }
  
  
          private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
          {
              UIElement thumb = e.Source as UIElement;
  
              Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
              Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
          }
  </code></pre>
  
  <p>Output:
  <a href="https://i.stack.imgur.com/RQaDF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RQaDF.jpg" alt="enter image description here"></a></p>
  
  <p>Your guidance is highly appreciated!
  Thanks!</p>