MapItemsControls不更新Silverlight Bing Map

时间:2010-03-11 05:34:20

标签: silverlight bing-maps pushpin

我正在使用MapItemsControl控制我的Bing silverlight地图中的Pushpin项目。

在页面加载时,我以编程方式添加新引脚,引脚显示在地图上。 但是我现在更进一步了,我通过点击地图为我的数据源添加了引脚。

新的引脚会添加到我的数据源中,但不会显示在地图上。我是否需要将我的数据源重新绑定到我的地图控件或以某种方式刷新数据源?这是一些代码

<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;
    }

1 个答案:

答案 0 :(得分:1)

您是否尝试将数据源包装到 ObservableCollection

// In constructor:
//
    ObservableCollection<MyData> data = new ObservableCollection<MyData>();
    mapItems.ItemsSource = data;

// At some other point in your code, such as a MouseClick handler.
//

    data.Add( pin );  // will update UI automatically.