我想根据我的ObservableCollection显示图钉:
XAML:
<Grid>
<my:MapControl>
<my:MapItemsControl ItemsSource="{Binding Users}">
<my:MapItemsControl.ItemTemplate>
<DataTemplate>
<my:MapIcon Title="{Binding Name}" Location="{Binding Location}"></my:MapIcon>
</DataTemplate>
</my:MapItemsControl.ItemTemplate>
</my:MapItemsControl>
</my:MapControl>
</Grid>
这里是ViewModel: public ObservableCollection Users {get;组; }
public MapPageViewModel()
{
Users = new ObservableCollection<User>()
{
new User
{
Id = 1,
Name = "Chris",
Location = new Location { Latitude = 52.645, Longitude = 13.431}
},
new User
{
Id = 1,
Name = "Brown",
Location = new Location { Latitude = 52.15, Longitude = 12.431}
}
};
}
在启动程序时尝试显示地图图标时出错,无论如何行中有错误
Title = {Binding Name}
即使User模型具有属性Name并且我将ItemSource设置为Users,也无法解析Name。
有谁知道如何解决这个问题?
答案 0 :(得分:0)
在xaml中使用此代码
<Grid>
<my:MapControl>
<my:MapItemsControl ItemsSource="{Binding Hairdressers}">
<my:MapItemsControl.ItemTemplate>
<DataTemplate>
<my:MapIcon Title="{Binding Name}" Location="{Binding Location}"></my:MapIcon>
</DataTemplate>
</my:MapItemsControl.ItemTemplate>
</my:MapItemsControl>
</my:MapControl>
</Grid>
指定您的Observable Collection名称,即理发师而非用户,因为您的收藏名称是理发师:
public ObservableCollection Hairdressers { get; set; }