开发者

Bing maps binding trouble

Im having a real headache binding my items to pushpins on a silverlight bing map.

Ive spent all day trying to get my collection sorted and now just cant get the pushpins to show up.

The items appear to be there as when you do a breakpoint on the last line as per the image below, all 143 items are there in _PushPins:

Bing maps binding trouble

Any help welcome. many thanks.

Here is the code:

 namespace observable_collection_test
{
public partial class Map : PhoneApplicati开发者_运维问答onPage
{

    public Map()
    {
        InitializeComponent();

        GetItems();
    }

    private ObservableCollection<SItem2> pushPins; 
    public ObservableCollection<SItem2> PushPins 
    {   
        get  { return this.pushPins; }   
        set   
        {     
         this.pushPins = value;

        this.OnPropertyChanged("PushPins");   
         } 
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }


 public void GetItems()
    {

         var document = XDocument.Load("ListSmall.xml");

         if (document.Root == null)
            return;

          var xmlns = XNamespace.Get("http://www.blah");

         var events = from ev in document.Descendants("item")
          select new
          {
           Latitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "lat").Value),
            Longitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "long").Value),

         };

         this.PushPins = new ObservableCollection<SItem2>();   
         foreach (var ev in events)   
          {     
         var pushPin = new SItem2(ev.Latitude, ev.Longitude);
         //Location = new GeoCoordinate(ev.Latitude, ev.Longitude )
         this.PushPins.Add(pushPin);   
         } 
}

Other class:

namespace observable_collection_test
{
 public class SItem2
{
           public double Latitude
    { get; set; }
    public double Longitude
    { get; set; }

    public SItem2(double Latitude, double Longitude)
    {
                    this.Latitude = Latitude;
        this.Longitude = Longitude;
              }

    public Location Location { get; set; }
}
}

XAML:

 <my:Map ZoomBarVisibility="Visible" ZoomLevel="10" CredentialsProvider="xxxxx"  Height="508" HorizontalAlignment="Left" Margin="0,22,0,0" Name="map1" VerticalAlignment="Top" Width="456" ScaleVisibility="Visible">
                  <my:MapItemsControl ItemsSource="{Binding PushPins}" >
                <my:MapItemsControl.ItemTemplate>
                    <DataTemplate>        
             <my:Pushpin Background="Aqua" Location="{Binding Location}" ManipulationCompleted="pin_click">
                        </my:Pushpin></DataTemplate>
                </my:MapItemsControl.ItemTemplate>
            </my:MapItemsControl>
 </my:Map>


You're trying to bind to a private field, _PushPins, instead of the public property PushPins. Also don't forget to implement INotifyPropertyChanged if you're changing the collection after binding.


I would do two things - firstly, implemented INotifyPropertyChanged and have your PushPins property use a private backing field. In the getter, just return the field value, in the setter, update the field value and invoke the PropertyChanged event.

Then, in your loop, rather than the getItems method (I would use PascalCase for method names) creating a local ObservableCollection, instantiate the PushPins collection and populate it directly.

private ObservableCollectin<SItem2> pushPins;
public ObservableCollection<SItem2> PushPins
{
  get { return this.pushPins; }
  set
  {
    this.pushPins = value;
    this.OnPropertyChanged("PushPins");
  }
}

public void GetItems()
{
  ...

  this.PushPins = new ObservableCollection<SItem2>();
  foreach (var ev in events)
  {
    var pushPin = new SItem2(ev.Latitude, ev.Longitude);
    this.PushPins.Add(pushPin);
  }
}

Populate the SItem2 Location property in the SItem2 constructor instead using the lat/long, and as Martin says, if you want the UI to update when you change the value of an SItem2 instance in the collection, then implement INotifyPropertyChanged on SItem2's properties also.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜