none
Binding with Bing Maps

    Question

  •  Hi,

     I'm developing a Silverlight app that uses the Bing Maps Services, I would like to write the code so it follows the MVVM pattern. To do that I need to be able to bind certain controls of the Bing Map such as Center, Zoomlevel, CredentialsProvider,...

      

    1        <Grid x:Name="LayoutRoot" Background="White" Height="550" Width="900">
    2            <bing:Map x:Name="map"  LogoVisibility="Collapsed" CopyrightVisibility="Collapsed" ZoomLevel="{Binding zoomLevel}" Center="{Binding locatie}" CredentialsProvider="{Binding bingKey}" ScaleVisibility="Collapsed" Margin="0,0,248,0" />
    3            <ListBox x:Name="lstWerknemers" ItemContainerStyle="{StaticResource lstStyle}" DisplayMemberPath="FullName" ItemsSource="{Binding lijstWerknemers}" Margin="658,0,0,298"/>
    4        </Grid>
    

     

    The DataContext in this .xaml page points to the following ViewModel:

      

    1    using BingMapsDatabaseMVVM.Web;
    2    using System.Collections.ObjectModel;
    3    using Microsoft.Maps.MapControl;
    4    using System.ComponentModel;
    5    
    6    namespace BingMapsDatabaseMVVM.ViewModels
    7    {
    8        public class BingMapsDatabaseMVVMViewModel : INotifyPropertyChanged
    9        {
    10           private ObservableCollection<Werknemer> _lijstWerknemers = new ObservableCollection<Werknemer>();
    11           private BingMapsDatabaseMVVMDomainContext _ctx;
    12           private ApplicationIdCredentialsProvider _bingKey = new ApplicationIdCredentialsProvider();
    13           private Location _locatie;
    14           private double _zoomLevel;
    15   
    16           #region properties
    17   
    18           public BingMapsDatabaseMVVMDomainContext context
    19           {
    20               get
    21               {
    22                   if (_ctx == null)
    23                   {
    24                       _ctx = new BingMapsDatabaseMVVMDomainContext();
    25                       return _ctx;
    26                   }
    27                   else
    28                   {
    29                       return _ctx;
    30                   }
    31               }
    32           }
    33   
    34           public ObservableCollection<Werknemer> lijstWerknemers 
    35           {
    36               get { return _lijstWerknemers;}
    37               set 
    38               {
    39                   _lijstWerknemers = value;
    40                   RaisePropertyChanged("lijstWerknemers"); 
    41               } 
    42           }
    43   
    44           public ApplicationIdCredentialsProvider bingKey
    45           {
    46               get { return _bingKey; }
    47               set
    48               {
    49                   _bingKey = value;
    50                   RaisePropertyChanged("bingKey");
    51               }
    52           }
    53   
    54           public Location locatie
    55           {
    56               get { return _locatie; }
    57               set 
    58               { 
    59                   _locatie = value;
    60                   RaisePropertyChanged("locatie");
    61               }
    62           }
    63   
    64           public double zoomLevel
    65           {
    66               get { return _zoomLevel; }
    67               set 
    68               { 
    69                   _zoomLevel = value;
    70                   RaisePropertyChanged("zoomLevel");
    71               }
    72           }
    73   
    74           #endregion
    75   
    76   
    77           public event PropertyChangedEventHandler PropertyChanged;
    78   
    79           public void RaisePropertyChanged(string propertyName) 
    80           { 
    81               if (PropertyChanged != null) 
    82               { 
    83                   PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    84               } 
    85           }
    86   
    87   
    88           public BingMapsDatabaseMVVMViewModel()
    89           {
    90               bingKey.ApplicationId = "(hidden)";
    91               zoomLevel = 9.0;
    92               locatie = new Location(50.908113, 3.372559);
    93               var query = context.Load<Werknemer>(context.GetWerknemersQuery());
    94               query.Completed += (send, args) =>
    95               {
    96                   lijstWerknemers = query.Entities.ToObservableCollection<Werknemer>();
    97               };
    98           }        
    99       }
    100  }
    

     As you may notice I also bind a listbox to an obserable collection and, to prove that the MVVM pattern I designed is OK, that works.

     

    Maybe someone here could help me out?

     

    Thanks in advance,

     

     

    Thomas

     

     

    Friday, March 26, 2010 12:12 PM

Answers

  •  After a day of searching I have finaly managed to solve the problem. The solution was to put the binding mode on TwoWay.

    Friday, March 26, 2010 12:40 PM