如何使用mvvmlight绑定图钉到bing地图

本文关键字:bing 地图 何使用 mvvmlight 绑定 | 更新日期: 2023-09-27 18:14:49

我试图使用MvvmLight工具包将我的自定义图钉模型绑定到bing地图控件。下面是我的代码:

    public class CustomPin
{
    public int Id { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

public List<CustomPin> Pins = new List<CustomPin>();

 private void BindLocation(List<Branch> branches)
    {
        for (int i = 0; i < branches.Count; i++)
        {
            CustomPin pin = new CustomPin();
            pin.Id = i;
            pin.Latitude = branches[i].Latitude;
            pin.Longitude = branches[i].Longitude;
            Pins.Add(pin);
        }
    }
我的XAML是:
       <bm:Map x:Name="Mymap"  ZoomLevel="1"  Credentials="xxxxx" Width="1366" Height="362">
        <bm:MapItemsControl x:Name="MapPins" ItemsSource="{Binding Pins}">
            <bm:Pushpin>
                <bm:MapLayer.Position>
                    <bm:Location Latitude="{Binding Path=Latitude}" Longitude="{Binding Path=Longitude}"></bm:Location>
                </bm:MapLayer.Position>
            </bm:Pushpin>
        </bm:MapItemsControl>
    </bm:Map>

我不能看到我的图钉当我运行这段代码。哪里出了问题?

如何使用mvvmlight绑定图钉到bing地图

您正在将值分配给列表_Pins,但您错误地将其绑定为Pins。所以将ItemSource设置为

ItemsSource="{Binding _Pins}"

希望能有所帮助。