views:

464

answers:

1

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>

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;
    }
+1  A: 

Have you tried wrapping your data source into an 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.
Adel Hazzah
Perfect, thank you for this! Works as expected now.
Matt