#729 – Mouse.GetPosition Doesn’t Work While Dragging

If you are handling the DragOver event during a drag-and-drop operation and you want to find the current mouse position, you need to use DragEventArgs.GetPosition, rather than the static Mouse.GetPosition method.

In the example below, we initiate a drag-and-drop operation in a window and then try reporting the mouse’s position in the window’s DragOver handler.  We try using both methods to get the mouse position, but only the DragEventArgs.GetPosition method works.

<Window x:Class="WpfApplication2.MainWindow"
        Title="Application 2" Height="350" Width="325"
        AllowDrop="True" DragOver="Window_DragOver">
        <Label Name="lblInfo1" Content="Info 1"/>
        <Label Name="lblInfo2" Content="Info 2"/>


        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
            DragDrop.DoDragDrop((DependencyObject)e.Source, "Sample", DragDropEffects.Copy);

        private void Window_DragOver(object sender, DragEventArgs e)
            System.Windows.Point p1 = Mouse.GetPosition(this);
            lblInfo1.Content = string.Format("Mouse.GetPosition: {0}, {1}", p1.X, p1.Y);

            System.Windows.Point p2 = e.GetPosition(this);
            lblInfo2.Content = string.Format("DragEventArgs.GetPosition: {0}, {1}", p2.X, p2.Y);


About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

2 Responses to #729 – Mouse.GetPosition Doesn’t Work While Dragging

  1. Pingback: Dew Drop – January 9, 2013 (#1,477) | Alvin Ashcraft's Morning Dew

  2. benlitz says:

    The GetPosition method is pretty broken when invoked from the DragLeave handler. Here’s an old post (not from me) describing the problem: https://social.msdn.microsoft.com/Forums/vstudio/en-US/d326384b-e182-4f48-ab8b-841a2c2ca4ab/whats-up-with-dragleave-and-egetposition?forum=wpf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: