This project has moved. For the latest updates, please go here.

Strange behaviour with MouseMove

May 20, 2013 at 9:19 PM
Edited May 20, 2013 at 9:22 PM
Hi,
i want a "pushpin" to follow the mouse and display some information at the mouse position. For testing I introduced some changes in the WPF sample application:

Added a readonly dependency property "MouseLocation" to the MainWindow class
internal static readonly DependencyPropertyKey MouseLocationKey = DependencyProperty.RegisterReadOnly("MouseLocation", typeof(Location), typeof(MainWindow), new PropertyMetadata(null));

public static readonly DependencyProperty MouseLocationProperty = MouseLocationKey.DependencyProperty;

Location Location { get { return GetValue(MouseLocationProperty) as Location; } }
Updated this property in the already available MouseMove event handler
var location = map.ViewportPointToLocation(e.GetPosition(map));
SetValue(MouseLocationKey, location);
Finally added a new pushpin in the XAML:
<map:Pushpin map:MapPanel.Location="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:MainWindow}}, Path=MouseLocation}" Background="Yellow" Foreground="Blue" Content="Follow me"/>
The problem is:
As long as you do not zoom or pan the map or resize the window in any way, the moving pushpin is only visible if you move the mouse the top edge of the map. The pushpin then sneaks in at the bottom edge The X-coordinate is well, the Y-coordinate seems to be off by exactly the heights of the map.

After you zoom or pan the map or resize the window the pushpin is tracked perfectly well at the mouse position and stays there.

Any ideas of what I am not doing right?

Environment: current code from codeplex ,Windows 7, VS-2012, .net-4.5 WPF Application
Coordinator
May 25, 2013 at 7:07 AM
The problem was that MapPanel arranges two kinds of child elements, those that have the Location attached property set and those that don't have.

Your Pushpin gets its Location value from a binding that produces a value after the Pushpin has already been arranged. As a Pushpin's default VerticalAlignment is Bottom, it gets arranged at the bottom of the map. When it later gets a value for the Location property, it isn't re-arranged. I've changed that in the current version 1.3.5. by re-arranging MapPanel child elements whenever they get an initial Location value, or when Location is reset to null.

However, as long as your Pushpin has no Location, it will still initially be arranged at the bottom left corner. You may perhaps assign a suitable default value for the MouseLocation property.
May 26, 2013 at 10:18 AM
Edited May 26, 2013 at 10:19 AM
Pulled the last version from Codeplex. Followed your advice. Problem is gone.

Thank you very much