How to Update MapView with Graphics via ViewModel?

1574
3
Jump to solution
10-31-2018 04:13 AM
KhurramAli
New Contributor II

I am developing an application with ArcGIS Runtime SDK for .Net and following the MVVM Pattern, in my ViewModel I have an ObservableCollection of GraphicsOverlay that I have binded to the MapView in my View, now when I add a new GraphicsOverlay to the ObservableCollection and add Graphics in it, graphics are not reflection in the View, 

I have implemented INotifyPropertyChanged and all other things are working fine with the ViewModel

        

public class MapViewModel : BaseViewModel
{
private Map map;

public Map Map
{
get { return this.map; }
set { this.map = value; }
}
public ObservableCollection<GraphicsOverlay> GraphicsOverlays { get; set; }

public MapViewModel()
{
GraphicsOverlays = new ObservableCollection<GraphicsOverlay>();
}

And in my Method that is called by any event

public void UpdateMarker(MapPoint point)
{
GraphicsOverlays[0].Graphics.Clear();

// Create a symbol to symbolize the point
SimpleMarkerSymbol symbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.X, System.Drawing.Color.Yellow, 20);

// Create the graphic
Graphic symbolGraphic = new Graphic(point, symbol);

// Add the graphic to the graphics overlay
var newGraphicsOverlay=new GraphicsOverlay();
GraphicsOverlays[0].Graphics.Add(symbolGraphic);

}

And in my View I have

<esri:MapView x:Name="MyMapView" Grid.Column="0" DataContext="{StaticResource MapVM}" GraphicsOverlays="{Binding GraphicsOverlays}" Map="{Binding Map}" Cursor="{Binding MapViewCursor}">

I am unable to find any sample that does exactly this, so how to do this, I am new to arcGIS, Thanks in advance.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoeHershman
MVP Regular Contributor

Use GraphicsOverlayCollection instead of ObservableCollection<GraphicsOverlay>

Thanks,
-Joe

View solution in original post

0 Kudos
3 Replies
JoeHershman
MVP Regular Contributor

Use GraphicsOverlayCollection instead of ObservableCollection<GraphicsOverlay>

Thanks,
-Joe
0 Kudos
JoeHershman
MVP Regular Contributor

To give a little more context an ObservableCollection responds to items being added or deleted to the collection.  So when bound to a list type control it will reflect those changes to the list.  With an ObservableCollection changes to the objects within the collection do not send notification (raise IPropertyChanged).  In normal C# if you wanted to have a bound list and also be able to see changes to the objects in that list you would use a BindingList.

With adding Graphics the Graphics property on the GraphicsOverlay changes, so there is no type of notification sent by the ObservableCollection.  The GraphicsOverlayCollection was provided to fire the proper notifications to see changes to the graphics (I'm assuming).

Cheers

Thanks,
-Joe
0 Kudos
KhurramAli
New Contributor II

Joe Hershman thanks for the insight. 

0 Kudos