1

I'm looking for advice on implementing drag & drop onto the WPF map control. My goal is to drag from a list of items, and drop on the map to create a new graphic/feature at the drop location.

The map has Drop and PreviewDrop events, but neither provide the map/mouse coordinates of the drop event. Am I missing something in the events? Both have DragEventArgs as the parameter.

All I really need is the mouse's map position when the drop happens. With that in mind, I also tried to get mouse move events, but they don't seem to carry any position either, only MouseEventArgs.

Any ideas? Am I missing something obvious?

Thanks!

P.S. ArcGIS Runtime 10.1.1, but I can upgrade to 10.2 if there is something that will help.

0

Got pointed in the right direction from Antti Kajanus on the ESRI forum. Here is the relevant Drop method:

private void Drop(object e)
{
    var args = e as DragEventArgs;
    if (args == null)
        return;

    var mousePosition = args.GetPosition(_map);
    var mapPoint = _map.ScreenToMap(mousePosition);

    if (_map.WrapAroundIsActive)
        mapPoint = Geometry.NormalizeCentralMeridian(mapPoint) as MapPoint;

    var graphic = new Graphic();
    graphic .Geometry = mapPoint;

    MyLayer.Graphics.Add(graphic );
}

I have posted a complete sample application at https://github.com/keithgemeinhart/ArcGis.Wpf.DragDropDemo

A couple of key points ....

I use the GalaSoft MVVM Light library to get an implementation of EventToCommand. The provides a mechanism for mapping an event to a command in the view model. For example:

<i:Interaction.Triggers >
   <i:EventTrigger EventName="Loaded">
    <cmd:EventToCommand Command="{Binding MapLoadedCommand, Source={StaticResource DragDropViewModel}}" PassEventArgsToCommand="True"/>
   </i:EventTrigger>
</i:Interaction.Triggers>

This maps the Loaded event on the map to the MapLoadedCommand in the DragDropViewModel. That's a key event because we need a means for getting a map reference into the view model. So my solution was to capture the map reference as a parameter of the map's Loaded event. Here is the implementation in the view model. The last line in the method extracts the map reference from the RoutedEventArgs Source property.

private void MapLoaded(object parameter)
{
    // See if the event sent the map in the event args
    var p1 = parameter as System.Windows.RoutedEventArgs;
    if (p1 == null)
    {
        throw new NullReferenceException("Cannot capture map reference.");
        return;
    }

    _map = p1.Source as Map;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.