我正在将一些图钉数据绑定到MapLayer。它们显示得很好,但是当我使用display命令从鼠标leftbuttonUp传递eventargs时,对象源是一个省略号。我在MapPolygon上使用了这个方法,并从对象中提取了我想要的信息。
也许我是mvvm的新手,所以请让我知道!
这适用于我的MapPolygons (vm引用我的extendedMapPolygon类的名称空间)
<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>我应该使用命令参数吗?或者当我单击图钉时将文本传递到视图模型的其他方式,这才是我真正想要的。
发布于 2011-05-27 04:36:50
我会把触发器移到图钉元件上。
<DataTemplate x:Name="ppTemplate">
<m:Pushpin ToolTipService.ToolTip="{Binding _psName}" Location="{Binding _Location}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<cmd:EventToCommand Command="{Binding Path=DataContext.pinSelCommand}"
CommandParameter="{Binding WhateverPropertyHasTheText" />
</i:EventTrigger>
</i:Interaction.Triggers>
</i:EventTrigger>
</i:Interaction.Triggers>
</m:Pushpin>
</DataTemplate>请注意,我将作为命令参数传递您想要从_PinsForDisplay中的对象发送的任何属性。此外,命令的绑定也略有变化,因为绑定与数据模板中的绑定不同。
然后,您必须将视图模型上的RelayCommand更改为RelayCommand。
我没有测试这段代码,但有一些非常类似的东西正在为我工作,所以希望它能引导您找到解决方案。
https://stackoverflow.com/questions/6042536
复制相似问题