2012-12-02 92 views
4

我试图使用Windows Phone工具包的地图API扩展的数据绑定。我做:绑定Windows Phone 8地图API扩展

<maps:Map x:Name="Map" Center="47.6, -122.3" ZoomLevel="12"> 
    <maptk:MapExtensions.Children> 
     <maptk:MapItemsControl ItemsSource="{Binding PositionList}"> 
      <maptk:MapItemsControl.ItemTemplate> 
       <DataTemplate> 
        <maptk:Pushpin GeoCoordinate="{Binding}" /> 
       </DataTemplate> 
      </maptk:MapItemsControl.ItemTemplate> 
     </maptk:MapItemsControl> 
    </maptk:MapExtensions.Children> 
</maps:Map> 

背后我的代码:?

public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

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

    private bool NotifyPropertyChanged<T>(ref T variable, T valeur, [CallerMemberName] string name= null) 
    { 
     if (object.Equals(variable, valeur)) return false; 

     variable = valeur; 
     NotifyPropertyChanged(name); 
     return true; 
    } 

    private IEnumerable<GeoCoordinate> positionList; 
    public IEnumerable<GeoCoordinate> PositionList 
    { 
     get { return positionList; } 
     set { NotifyPropertyChanged(ref positionList, value); } 
    } 

    public MainPage() 
    { 
     InitializeComponent(); 
     PositionList = new List<GeoCoordinate> 
     { 
      new GeoCoordinate(47.6050338745117, -122.334243774414), 
      new GeoCoordinate(47.6045697927475, -122.329885661602), 
      new GeoCoordinate(47.605712890625, -122.330268859863), 
      new GeoCoordinate(47.6015319824219, -122.335113525391), 
      new GeoCoordinate(47.6056594848633, -122.334243774414) 
     }; 

     DataContext = this; 
    } 
} 

但我不能看地图:(

什么我做错了任何图钉

请注意,如果我在代码隐藏文件中使用它,它正在工作

MapExtensions.GetChildren(Map).OfType<MapItemsControl>().First().ItemsSource = PositionList; 

在此先感谢您的帮助,

问候

+0

我有同样的问题。无法通过仅限xaml文件获取绑定。 – likebobby

回答

4

MapItemsControl自DependencyObject派生,而不是FrameworkElement的这样的DataContext不传播。长话长......除非你有办法设置Binding的Source属性,否则你不能从XAML绑定MapItemsControl。

如果RelativeSource的FindAncestor模式在手机上工作,可能可以解决这个问题,但它显然不能。这让我们无论是在代码中创建绑定还是(更现实地)在代码中设置ItemsSource。