4

I have a WP Phone app using a Bing Map control. I have an array of objects, and each object has a location. I iterate the array to place the pins on the map (see below). I have a touch event bound to each pin to allow the user to tap the pin to start an action.

Now - I would like, on tap, to show information from the object that relates to that pin to be shown in a textbox. How can I retrieve the object from the array that corresponds to the pushpin that was tapped/clicked?

foreach (wikiResult result in arrayResults)
{
    double lat = double.Parse(result.Latitude, CultureInfo.InvariantCulture);
    double lng = double.Parse(result.Longitude, CultureInfo.InvariantCulture);
    statusTextBlock.Text = result.Latitude + "  " + result.Longitude + "  " + lat + "  " + lng;

    GeoCoordinate d = new GeoCoordinate(lat, lng);

    Pushpin pin;
    pin = new Pushpin();
    pin.Location = d;
    pin.Content = result.Name;
    pin.MouseLeftButtonUp += new MouseButtonEventHandler(pin1_MouseLeftButtonUp);
    myMap.Children.Add(pin);
}

void pin1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    //display the content from the object in a text box
}  

Many thanks in advance!

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
Will Gill
  • 577
  • 2
  • 10
  • 21

1 Answers1

7

The sender is the Pushpin so you can simply typecast it:

var pushpin = sender as Pushpin

And then you can access it's content. If you need more detailed binding, use the Tag property of the Pushpin.

Also, I would suggest you use the Tap event on the Pushpin, if you're using Windows Phone 7.1 (Mango). And I would also recommend that you consider using databindings, instead of manually adding the items from C#.

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • 1
    typecasting the Pushpin did the trick. Thanks. Will also update to the Tap event for Mango. Do you have a link, by chance, to somewhere that describes databindings for the pushpins? – Will Gill Sep 01 '11 at 05:07