问题
I'm trying to create a pushpin in a Silverlight Bing Map that is both draggable as well as clickable. I found some code here http://pietschsoft.com/post/2010/05/30/Draggable-Pushpins-using-Bing-Maps-Silverlight-Control.aspx to achieve the draggable part.
I've mapped mouseclick on the map to drop a pin.
However, the problem (and this exists with the default pushpin as well) is that clicking on the pushpin seems to be handled by the map control first so I end up dropping another pin, instead of registering an event on the pushpin.
If I replace the pin with a ControlTemplate that is a button, I can trap the click, but now I don't have a draggable pushpin.
Can anyone recommend a solution ?
回答1:
I've found this approach to work. Recording it here so that it helps someone else.
- Create a Content Template for the DraggablePushpin from above link so that it only contains a DraggableButton(below).
inherit from Button to create a DraggableButton (referenced in the template above)
In this DraggableButton, override the OnMouseLeftButtonDown and the OnMouseLeftButtonUp and pass them to the DraggablePushpin. I created a property called ThePin on the DraggableButton and set that in the OnApplyTemplate method of the DraggablePushpin. You can call GetTemplateChild() in OnApplyTemplate to get a reference to the DraggableButton instance in the pushpin.
The DraggableButton can now override the Click event for custom actions and also pass through the other events to the DraggablePushpin so that it works as expected.
There maybe a cleaner way to do this, but this is what worked for me.
来源:https://stackoverflow.com/questions/5712470/draggable-and-clickable-pushpin-in-silverlight-bing-map