I'm using the Bing Maps control to display a series of custom pushpins, representing objects which are constantly moving around and changing state.
Using databinding the various properties for the objects are being updated as they should, but for some reason this doesn't apply to their locations.
I have the map bound to an ObservableCollection as such:
<UserControl.Resources>
<DataTemplate x:Key="PushpinTemplate">
<v:CustomPushpin />
</DataTemplate>
</UserControl.Resources>
...
<m:Map Name="map">
<m:MapItemsControl ItemTemplate="{StaticResource PushpinTemplate}" ItemsSource="开发者_JS百科{Binding Objects}" />
</m:Map>
...and in CustomPushpin:
<UserControl
...
m:MapLayer.Position="{Binding Location}" m:MapLayer.PositionOrigin="BottomCenter"
mc:Ignorable="d" d:DesignHeight="126" d:DesignWidth="85">
Like all other properties, the Location of the individual objects are implemented using INotifyPropertyChanged.
private Location _location;
public Location Location
{
get { return _location; }
set { _location = value; OnPropertyChanged("Location"); }
}
When the map is moving, either because of panning or zooming, the objects move, but othersize not.
I can't quite figure if I'm doing something wrong here, or if it's a problem with the Bing Maps control.
Any thought?
I found a workaround by Candritzky here: Solution link with code
Basically you'll have to change the ItemsPanel
to custom one that expose new AttachedProperty
. For me it works :)
Do not update the location, like this:
_pushPin.Location.Latitude = xyz...
instead create a new object:
_pushPin.Location = new Location(_location);
otherwise BingMap
will somehow lose track of the update eventually
refreshing view by
map.SetView(map.BoundingRectangle);
works
Refreshing view didn't work for me. Instead of recreate Pushpin, the simplest solution was :
_pushPin.Location = null;
_pushPin.Location = _location;
or
_pushPin.Location = new Location(_location);
Hope this helps.
I found something here: it seems like there's a bug in the Bing's Map Control from Microsoft. I even tried the suggested workaround, but it's not working for me
In case anyone wonders, my solution was to create my own class ObjectCollection
and inherit from ObservableCollection<Object>
. Add a method ObjectCollection.Refresh() like so:
public void Refresh()
{
base.OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
}
and call this method after I'm done repopulating the collection in a loop.
精彩评论