I am creating an app where I am using MvvmCross on WP7 along with a Bing Maps control. I am trying to bind the MapItemsControl ItemSource to an ObservableCollection of LocationDataSource, which contain a property MapPosition of the type GeoPoint. GeoPoint is a simple class which contain information about the Latitude and Longitude of the location.
The locations which I populate into the ObservableCollection are fetched by a service which my ViewModel listens to and updates the collection when it has fetched the locations. Though it does not seem to work as no locations are shown on the map.
To bind the GeoPoints I have made a converter which simply converts to GeoCoordinate which the MapItemsControl understands. I know the converter works as I am using it to bind to the Map Center property.
The binding looks like this:
<maps:MapItemsControl x:Name="mapControl" ItemsSource="{Binding Locations}">
<maps:MapItemsControl.ItemTemplate>
<DataTemplate>
<maps:Pushpin Location="{Binding MapPosition, Converter={StaticResource GeoPoint}}" Template="{StaticResource pinSiteLoc}" />
</DataTemplate>
</maps:MapItemsControl.ItemTemplate>
</maps:MapItemsControl>
The ViewModel looks like this:
private ObservableCollection<LocationDataModel> _locations;
public ObservableCollection<LocationDataModel> Locations
{
get { return _locations; }
set
{
_locations = value;
FirePropertyChanged(() => Locations);
}
}
I have tried various ways to update the Locations collection, which is done in the ViewModel in an event handler after the ViewModel has loaded and is ready, I assumed this would be working:
Locations = LocationDataService.Locations;
I have checked that LocationDataService.Locations is not empty and contains an actual location.
I also tried intantiating the collection the the ViewModel constructor and then add each element to the collection, which did not show anything on the map either.
EDIT
As per request in the comments. I have tried adding the locations like this to the collection, where it was instantiated as Locations = new ObservableCollection<LocationDataModel>(); in the ViewModel constructor. Then in the event handler it was populated as such:
private void LocationDataServiceOnLoadingChanged(object sender, EventArgs eventArgs)
{
if (LocationDataService.IsInventoryLoaded)
{
foreach (var location in LocationDataService.Locations)
{
Locations.Add(location);
}
}
}
Can anyone see what I am doing wrong here?
As stated in the comment I made to the question, I found an error in the xaml file, where I prematurely closed the
Mapcontrol tag like so:Instead it had to be: