Search code examples
windows-phone-7dictionarybingpushpin

How to get the model object of a pushpin through a click on the pushpin in bing map?


My pushpins are added to the map in a common way like this:

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>

On clicking a pushpin i can get the Pushpin as sender and get Location information. But i would like to track down to it's PushpinModel object to get other informations associated with the Pushpin, like name, description, urls, etc. How can i do that?


Solution

  • First of all, you should use the Tap event, instead of MouseLeftButtonUp.

    Secondly, the sender in the event handler, is the Pushpin, and it's DataContext property is your bound PushpinModel, so simply do:

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