2

私のアイテムをシルバーライトのビンマップ上のプッシュピンにバインドするのに本当の頭痛があります。ビングマップバインディングのトラブル

Iveは私のコレクションをソートしようと一日中過ごしましたが、今はちょうど表示されないようにプッシュピンを取得します。

項目は、下の画像の通り、最後の行にブレークポイントを実行するときのようにそこに表示され、すべての143個の項目が_PushPinsにあります

alt text

任意のヘルプを歓迎。どうもありがとう。

namespace observable_collection_test 
{ 
public partial class Map : PhoneApplicationPage 
{ 

    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); 
     } 
} 

その他のクラス:

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> 

答えて

2

あなたがプライベートフィールドにバインドしようとしている、_PushPins、代わりにここで

コードですパブリックプロパティPushPins。バインド後にコレクションを変更する場合は、INotifyPropertyChangedを実装することも忘れないでください。

+0

答えていただきありがとうございます。私は自分のコードと画像を編集して、問題と思われるものにしました。まだ喜びはありません。 ObservableCollection PushPinsのために、ある種のアクセサリを取得する必要がありますか? –

+1

はい、プロパティはバインドできず、フィールドはバインドできません。また、XAMLは依然として_PushPinsを参照しています。 – devdigital

+0

XAMLを使用しているAhhは、遊んでいる間違いでした。学童エラー。 私は、get/setアクセサで何をしているのか、iveはそれを編集したのか、それでもまだ喜んではいません:(確かに、専門家には分かりません.-) –

1

最初に、INotifyPropertyChangedを実装し、プッシュピンプロパティにプライベートバッキングフィールドを使用させます。 getterでは、フィールド値をセッターで返し、フィールド値を更新してPropertyChangedイベントを呼び出します。

次に、ローカルのObservableCollectionを作成するgetItemsメソッド(私はメソッド名にPascalCaseを使用します)ではなくループ内で、PushPinsコレクションをインスタンス化して直接作成します。

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); 
    } 
} 

あなたがコレクションにSItem2インスタンスの値を変更したときにUIを更新する場合は実装し、その後、緯度/経度を使用して代わりにSItem2コンストラクタでSItem2 Locationプロパティを移入し、マーティンが言うようにSItem2のプロパティでもINotifyPropertyChanged。

+0

これまでのご協力ありがとうございます。コードを自分の現在の状態に更新しました。私は、「Location =新しいGeoCoordinate(ev.Latitude、ev.Longitude)」を使って何をしているのか不明確です。私の知識の欠如がここに現れていることを確かめてください。 –

+0

SItem2コンストラクタ内でLocationの値をどこにも設定していないので、this.Location = new Location(this.Latitude、this.Longitude); – devdigital

+0

ありがとう、私はそれを試してみます。 –