1
votes

I'm having a bit of trouble extracting some information I need when I pass a data binding in Xaml for a windows 8 app.

I have a default template designed in the app.xaml here:

 <DataTemplate x:Key="PinTemplate">
            <bm:Pushpin Name="{Binding img}" IsTapEnabled="True" >
                <bm:MapLayer.Position>
                    <bm:Location Latitude="{Binding Latitude}" Longitude="{Binding Longitude}" />
                </bm:MapLayer.Position>
            </bm:Pushpin>
 </DataTemplate>

I want to access the individual "img" strings from the binding above. On my map I have an itemcontrol embedded like so:

 <Maps:Map Name="london" HorizontalAlignment="Left" Height="546" Margin="78,34,0,0" Grid.Row="1"       VerticalAlignment="Top" Width="806" Credentials="{StaticResource BingCredentials}" RenderTransformOrigin="0.439,0.282">
        <Maps:Pushpin Name="myPin" Height="100" Width="100"/>
        <Maps:MapItemsControl ItemTemplate="{StaticResource PinTemplate}" ItemsSource="{Binding PushpinCollection}" Height="100" Width="100" Tapped="pushPin_Tapped"/>
        <Popup VerticalOffset="200"  HorizontalOffset="300" x:Name="Image" IsLightDismissEnabled="True" Height="2000" Width="2000" >
            <Image Name="myImage" Height="300" Width="300" Source="Assets/Logo.png"/>
        </Popup>
    </Maps:Map>

And the c# behind it is:

 public class PushpinModel
    {
        public double Longitude { get; set; }
        public double Latitude { get; set; }
        public string img { get; set; }
    }

  public ObservableCollection<PushpinModel> PushpinCollection { get; set; }

  PushpinCollection = new ObservableCollection<PushpinModel>();

        PushpinCollection.Add(new PushpinModel() { Latitude = templat[0], Longitude = templng[0], img = names[0] });
        PushpinCollection.Add(new PushpinModel() { Latitude = templat[1], Longitude = templng[1], img = names[1] });
        DataContext = this;

As it stands now I have an action control "Tapped" on the MapsItemcontrol which works properly, it creates a popup and displays a picture. However I would like to pass the information in img to the action controller so that I can specify which image to display in specific pushpins. I tried to do it like below but I think it the data context it returns is for the itemscontrol as a whole, how do I access the properties within the data template for each individual instance of pushpincollection? Thanks

     private void pushPin_Tapped(object sender, TappedRoutedEventArgs e)
    {
           if (!Image.IsOpen) { Image.IsOpen = true; }
              var pushpinData = (sender as Pushpin).DataContext as PushpinModel;
              String file = pushpinData.ToString();
              // use image in popup here
    }
1

1 Answers

0
votes

Try

var pushpinData = (sender as FrameworkElement).DataContext as PushpinModel;

That's how I usually do it, cause you don't always know what the sender type is but to me it seems it is the Pushpin..