我正在将一些图钉数据绑定到 MapLayer。它们显示得很好,但是当我使用中继命令从鼠标左键向上传递事件参数时,对象源是一个椭圆。我在 MapPolygon 上使用了这种方法,并从对象中获取了我想要的信息。
由于我是 mvvm 的新手,所以我可能会遇到这种情况,所以请告诉我!
这适用于我的 MapPolygons(vm 引用了我的扩展 MapPolygon 类的命名空间)
<DataTemplate x:Name="polyTemplate">
<vm:extendedMapPolygon cName="{Binding _cName}" Locations="{Binding _Locations}" />
</DataTemplate>
这是 MapLayer 中的 XAML
<m:MapItemsControl ItemTemplate="{StaticResource polyTemplate}" ItemsSource="{Binding Path=_PolyforDisplay, Mode=TwoWay}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<cmd:EventToCommand Command="{Binding Path=PolySelCommand}" PassEventArgsToCommand="True" ></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</m:MapItemsControl>
在我的 viewModel 构造函数中
PolySelCommand = new RelayCommand<MouseButtonEventArgs>(PolySelCommandExecute);
最后是实际的命令
public RelayCommand<MouseButtonEventArgs> PolySelCommand { get; set; }
private void PolySelCommandExecute(MouseButtonEventArgs cmp)
{
Polygon poly = cmp.OriginalSource as Polygon;
extendedMapPolygon ePoly = poly.Parent as extendedMapPolygon;
_MapPolygonSelected = ePoly.cName;
}
(我把它放在这里是为了展示我目前使用的方法,并希望它对其他人有用!)
然而,当我用图钉尝试同样的事情时, cmp.OriginalSource 是一个椭圆,我似乎无法通过其他任何东西。
我的图钉代码(我只是在此代码中使用 MapControl 中的图钉)
<DataTemplate x:Name="ppTemplate">
<m:Pushpin ToolTipService.ToolTip="{Binding _psName}" Location="{Binding _Location}" />
</DataTemplate>
<m:MapItemsControl ItemTemplate="{StaticResource ppTemplate}" ItemsSource="{Binding Path=_PinsforDisplay, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<cmd:EventToCommand Command="{Binding Path=pinSelCommand}" PassEventArgsToCommand="True" ></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</m:MapItemsControl>
我应该使用命令参数吗?或者当我单击图钉时将文本传递到我的视图模型的其他方式,这是我真正想要的。