MapItemsControls 不更新 Silverlight Bing 地图
我正在使用 MapItemsControl 来控制 Bing silverlight 地图中的图钉项目。
在页面加载时,我以编程方式添加一个新图钉,并且图钉显示在地图上。 不过,我现在更进一步,通过单击地图将图钉添加到我的数据源。
新的图钉添加到我的数据源,但没有显示在地图上。我是否需要将数据源重新绑定到地图控件或以某种方式刷新数据源?这是一些代码
<UserControl.Resources>
<DataTemplate x:Key="PinData">
<m:Pushpin Location="{Binding Location}" PositionOrigin="BottomCenter" Width="Auto" Height="Auto" Cursor="Hand">
<m:Pushpin.Template>
<ControlTemplate>
<Grid>
<myTestApp:MasterPin DataContext="{Binding}"/>
</Grid>
</ControlTemplate>
</m:Pushpin.Template>
</m:Pushpin>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<m:Map x:Name="myMap" CredentialsProvider="" Mode="Road" ScaleVisibility="Collapsed" >
<m:MapItemsControl x:Name="mapItems" ItemTemplate="{StaticResource PinData}"/>
</m:Map>
</Grid>
后面的代码:
public partial class Map : UserControl
{
private List< BasePin > dataSource = new List< BasePin >();
public Map()
{
InitializeComponent();
_Initialize();
}
private void _Initialize()
{
//this part works and adds a pin to the map
dataSource.Add( new BaseSite( -33.881532, 18.440208 ) );
myMap.MouseClick += Map_MouseClick;
mapItems.ItemsSource = dataSource;
}
public void Map_MouseClick(object sender, MapMouseEventArgs e))
{
BasePin pin = new BasePin();
pin.Location = myMap.ViewportPointToLocation( e.ViewportPoint );
dataSource.Add( pin );
}
}
--UPDATE
似乎如果将我的 mapItems.ItemSource 设置为 null,然后返回到 dataSource 对象,它就可以工作......但为什么呢?
public void Map_MouseClick(object sender, MapMouseEventArgs e))
{
BasePin pin = new BasePin();
pin.Location = myMap.ViewportPointToLocation( e.ViewportPoint );
dataSource.Add( pin );
mapItems.ItemSource = null;
mapItems.ItemSource = dataSource;
}
I'm using a MapItemsControl to control my Pushpin items within my Bing silverlight map.
Right on the page load, I add a new pin programatically, and the pin shows up on the map.
However I've now taken it further and I'm adding pins to my datasource via a click on the map.
The new pins add to my datasource, but do not show up on the map. Do I need to rebind my datasource to my map control or somehow refresh the datasource? Here's some code
<UserControl.Resources>
<DataTemplate x:Key="PinData">
<m:Pushpin Location="{Binding Location}" PositionOrigin="BottomCenter" Width="Auto" Height="Auto" Cursor="Hand">
<m:Pushpin.Template>
<ControlTemplate>
<Grid>
<myTestApp:MasterPin DataContext="{Binding}"/>
</Grid>
</ControlTemplate>
</m:Pushpin.Template>
</m:Pushpin>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<m:Map x:Name="myMap" CredentialsProvider="" Mode="Road" ScaleVisibility="Collapsed" >
<m:MapItemsControl x:Name="mapItems" ItemTemplate="{StaticResource PinData}"/>
</m:Map>
</Grid>
And the code behind:
public partial class Map : UserControl
{
private List< BasePin > dataSource = new List< BasePin >();
public Map()
{
InitializeComponent();
_Initialize();
}
private void _Initialize()
{
//this part works and adds a pin to the map
dataSource.Add( new BaseSite( -33.881532, 18.440208 ) );
myMap.MouseClick += Map_MouseClick;
mapItems.ItemsSource = dataSource;
}
public void Map_MouseClick(object sender, MapMouseEventArgs e))
{
BasePin pin = new BasePin();
pin.Location = myMap.ViewportPointToLocation( e.ViewportPoint );
dataSource.Add( pin );
}
}
--UPDATE
It seems that if set my mapItems.ItemSource to null, and then back to the dataSource object it works...but why?
public void Map_MouseClick(object sender, MapMouseEventArgs e))
{
BasePin pin = new BasePin();
pin.Location = myMap.ViewportPointToLocation( e.ViewportPoint );
dataSource.Add( pin );
mapItems.ItemSource = null;
mapItems.ItemSource = dataSource;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过将数据源包装到 ObservableCollection 中?
Have you tried wrapping your data source into an ObservableCollection?