WPF Bingmaps图钉事件绑定mvvm

时间:2013-12-16 14:15:32

标签: wpf mvvm bing-maps

我想在地图上点击图钉时在我的ViewModel中触发一个命令。如何使用数据绑定实现此目的?

以下是我用于精确定位的DataTemplate

<DataTemplate x:Key="PushPinTemplate">
    <map:Pushpin Cursor="Hand"
                 map:MapLayer.Position="{Binding Location}">
    </map:Pushpin>
</DataTemplate>

1 个答案:

答案 0 :(得分:2)

一位同事找到了解决方案:

添加Nuget包System.Windows.Interactivity.WPF后 并添加xml命名空间

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

您可以在模板中添加EventTrigger,此处为完整的模板代码 CachePushPinClickedICommand

<DataTemplate x:Key="PushPinTemplate">
    <map:Pushpin Cursor="Hand"
                     map:MapLayer.Position="{Binding Location}" >   
     <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonDown">
            <i:InvokeCommandAction Command="{Binding CachePushPinClicked}"/>
        </i:EventTrigger>
     </i:Interaction.Triggers>
   </map:Pushpin>
</DataTemplate>