2013-05-07 22 views
6

に画鋲のコレクションを追加するには、XAMLコードです。 トレックObservableCollection<PushPinModel>です。このコードを実行すると、UserLocationMarkerのみが表示されます。これは現在の場所です。MVVMのWindows Phone 8マップここ

答えて

16

私はついにそれを依存プロパティを使って動作させます。私は、新しいクラスを追加しました:

public static class MapPushPinDependency 
{ 
    public static readonly DependencyProperty ItemsSourceProperty = 
      DependencyProperty.RegisterAttached(
      "ItemsSource", typeof(IEnumerable), typeof(MapPushPinDependency), 
      new PropertyMetadata(OnPushPinPropertyChanged)); 

    private static void OnPushPinPropertyChanged(DependencyObject d, 
      DependencyPropertyChangedEventArgs e) 
    { 
     UIElement uie = (UIElement)d; 
     var pushpin = MapExtensions.GetChildren((Map)uie).OfType<MapItemsControl>().FirstOrDefault(); 
     pushpin.ItemsSource = (IEnumerable)e.NewValue; 
    } 


    #region Getters and Setters 

    public static IEnumerable GetItemsSource(DependencyObject obj) 
    { 
     return (IEnumerable)obj.GetValue(ItemsSourceProperty); 
    } 

    public static void SetItemsSource(DependencyObject obj, IEnumerable value) 
    { 
     obj.SetValue(ItemsSourceProperty, value); 
    } 

    #endregion 
} 

そして、私は

xmlns:dp="clr-namespace:Treks.App.Util.DependencyProperties" 

を追加したの.xamlファイルの中に、今の.xamlファイルには、次のようになります。すべての画鋲今

<maps:Map x:Name="NearbyMap" 
        Center="{Binding MapCenter, Mode=TwoWay}" 
        ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}" 
        dp:MapPushPinDependency.ItemsSource="{Binding Path=Treks}" 
       > 
     <maptk:MapExtensions.Children> 
      <maptk:MapItemsControl Name="StoresMapItemsControl"> 
       <maptk:MapItemsControl.ItemTemplate> 
        <DataTemplate> 
         <maptk:Pushpin x:Name="PushPins" GeoCoordinate="{Binding Location}" Visibility="Visible" Content="test"/> 
        </DataTemplate> 
       </maptk:MapItemsControl.ItemTemplate> 
      </maptk:MapItemsControl> 
      <maptk:UserLocationMarker x:Name="UserLocationMarker" Visibility="Visible" GeoCoordinate="{Binding MyLocation}"/> 
     </maptk:MapExtensions.Children> 
    </maps:Map> 

正しく表示されます。

+0

これは私の問題を解決しました。優れたアプローチ – xximjasonxx

+0

ありがとうございました。 – robertk

+0

Page.xaml.csのコードを提供することもできますか?地図アイテムの実際のリストをどのようにバインドできるかはわかりません')を' Map'または 'MapItemsControl'に追加しました。 –

3

MapItemsControlは現在MVVMバインド可能ではありません。 あなたのビューのコードの後ろにItemsSourceを設定するのが一番良い方法です。

あなたのViewModelで定義されたコレクションを使用することはできますが、 オプションは次のとおりです。this.StoresMapItemsControl.ItemsSource = ServiceLocator.Current.GetInstance<MainViewModel>().Locations; :MVVMメッセージングを通じて

  • は、ビュー
  • の背後にあるコードのコレクションにアクセスするには、ビューのDataContextのを使用するのviewmodelからコレクションに沿ってこのような何かを渡します
関連する問題