data-binding - 与 Windows Phone 8 的 map API 扩展绑定(bind)

标签 data-binding windows-phone-8 toolkit

我正在尝试将数据绑定(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/

相关文章:

javascript - polymer 数据绑定(bind)到方法

c# - 在 C# (winforms) 中如何同步文本框和 datagridview,以便一个中的更改显示在另一个中

c - 是否有不需要使用自己的事件循环的 GUI C 库?

AngularJS:将对象的属性绑定(bind)到 ngModel

c# - 如何在后台逐 block 上传文件?

c# - C# 中与 Windows Phone 的 Web 服务和服务器客户端连接

c# - 如何检查WP8设备是否使用wifi,移动套餐或漫游加载数据

java - 从命令行在 Java 代码中进行更高级别的语义搜索和替换

javascript - ASP.Net Ajax 工具包客户端代码和 javascript 问题

c# - WinForm 控件绑定(bind)到 List<T> 问题