Reputation: 557
I have a cluster of pushpins and i have added the click events for all the pushpins in foreach. now i need to find out which pushpin is clicked so as to do the actions accordingly. Below is my sample code.
private void setpins()
{
Pushpin pin = null;
lstpin.Add(new clsPushpin() { stores = "chennai", _loc= new locations() { lat = 13.04, longd = 80.17 } });
lstpin.Add(new clsPushpin() { stores = "Karur", _loc = new locations() { lat = 10.58, longd = 78.07 } });
lstpin.Add(new clsPushpin() { stores = "coimbatore", _loc = new locations() { lat = 11.00, longd = 77.00 } });
foreach (clsPushpin cls in lstpin)
{
pin = new Pushpin();
GeoCoordinate geo = new GeoCoordinate(cls._loc.lat, cls._loc.longd);
pin.Location = geo;
pin.Background = new SolidColorBrush(new Color() { A = 255, R = 0, G = 100, B = 120 });
pin.Foreground = new SolidColorBrush(Colors.White);
mymap.Children.Add(pin);
pin.MouseLeftButtonUp += new MouseButtonEventHandler(pushpintap);
}
mymap.Center = pin.Location;
mymap.SetView(pin.Location, 5.0);
}
private void pushpintap(object sender, MouseButtonEventArgs e)
{
//Messagebox are what ever
MessageBox.Show("My lat long is:"+lat,+long);
}
With the above snippet,the last pushpin's value is saved. But i wanna find the exact pin which gets selected to notify/pop up accoringly. Thx in advance.
Upvotes: 1
Views: 1613
Reputation: 714
I was having a similar problem and looking for a solution I found your question. What I did was to define a method that created a MouseButtonEventHandler
dynamically. For example if you want to show information about each cls
object you can define the next method:
public MouseButtonEventHandler getHandler(clsPushpin cls)
{
return delegate(object sender, MouseButtonEventArgs e)
{
MessageBox.Show(cls.stores+" at "+cls._loc.lat+","+cls._loc.longd);
};
}
and call it each time you create/add a Pushpin
:
pin.MouseLeftButtonUp += getHandler(cls);
[I tried doing it directly without defining the getHandler
method (that is: pin.MouseLeftButtonUp += delegate(...
) and it didn't work]
Upvotes: 0
Reputation: 868
You could do the following:
Store the clsPushpin object in the Pushpin Tag property.
In the pushpin click event cast the sender.tag as a clsPushpin object to get the data for that pushpin.
foreach (clsPushpin cls in lstpin)
{
pin = new Pushpin();
GeoCoordinate geo = new GeoCoordinate(cls._loc.lat, cls._loc.longd);
pin.Location = geo;
pin.Background = new SolidColorBrush(new Color() { A = 255, R = 0, G = 100, B = 120 });
pin.Foreground = new SolidColorBrush(Colors.White);
mymap.Children.Add(pin);
pin.MouseLeftButtonUp += new MouseButtonEventHandler(pushpintap);
pin.Tag = cls;
}
Then in your pushpintap event handler do this:
private void pushpintap(object sender, MouseButtonEventArgs e)
{
//Messagebox are what ever
clsPushpin cls = sender.tag as clsPushpin;
MessageBox.Show("My lat long is:"+cls.lat.ToString()+","+cls.long.ToString());
}
You probably should be using databinding to bind the data to a collection of pushpins.
Upvotes: 2
Reputation: 65564
You should query the sender
in the pushpintap
event to find out the details of the pin that was tapped.
Upvotes: 1