MapItemsControls 不更新 Silverlight Bing 地图

发布于 2024-08-24 21:24:03 字数 2166 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱你一生 2024-08-31 21:24:03

您是否尝试过将数据源包装到 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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文