我正在尝试将数据绑定(bind)与 windows phone 工具包的 map 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;
}
}
但我在 map 上看不到任何图钉:(
我究竟做错了什么 ?
请注意,如果我在代码隐藏文件中使用它,它就可以工作
MapExtensions.GetChildren(Map).OfType<MapItemsControl>().First().ItemsSource = PositionList;
在此先感谢您的帮助,
最好的祝福
最佳答案
MapItemsControl 派生自 DependencyObject,而不是 FrameworkElement,因此 DataContext 不会传播。长话短说...除非您有某种方法可以设置绑定(bind)的 Source 属性,否则您无法从 XAML 数据绑定(bind) MapItemsControl。
如果RelativeSource 的FindAncestor 模式在手机上工作,也许可以解决这个问题,但显然不行。这让我们要么在代码中创建绑定(bind),要么(更实际地)在代码中设置 ItemsSource。
关于data-binding - 与 Windows Phone 8 的 map API 扩展绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13668929/