如何通过点击bing map中的图钉来获取图钉的模型对象?

时间:2012-01-25 10:52:16

标签: windows-phone-7 map bing pushpin

我的图钉以常见的方式添加到地图中:

C#:

private readonly ObservableCollection<PushpinModel> _pushpins = new observableCollection<PushpinModel>();

public ObservableCollection<PushpinModel> Pushpins
{
    get{return _pushpins;}
}

XAML:

<my:MapItemsControl ItemsSource="{Binding Pushpins}">
    <my:MapItemsControl.ItemTemplate>
        <DataTemplate>
            <my:Pushpin Style="{StaticResource PushpinStyle}" 
                        MouseLeftButtonUp="Pushpin_MouseLeftButtonUp" 
                        Location="{Binding Location}" 
                        Content="{Binding Num}">
            </my:Pushpin>
        </DataTemplate>
    </my:MapItemsControl.ItemTemplate>
</my:MapItemsControl>

点击图钉后,我可以将Pushpin作为发件人并获取位置信息。 但我想跟踪它的PushpinModel对象以获取与Pushpin相关的其他信息,如名称,描述,网址等。我该怎么做?

1 个答案:

答案 0 :(得分:3)

首先,您应该使用Tap事件,而不是MouseLeftButtonUp

其次,事件处理程序中的senderPushpin,它的DataContext属性是您绑定的PushpinModel,所以只需执行:< / p>

var pushpinModel = (sender as Pushpin).DataContext as PushpinModel;