/// <summary> /// Delete graphic object from layer. /// </summary> /// <param name="data">Data object to delete.</param> /// <param name="layer">Graphics layer from which to delete.</param> public static void DeleteObject(object data, GraphicsLayer layer) { for (int index = layer.Graphics.Count - 1; index >= 0; index--) { DataGraphicObject graphic = (DataGraphicObject)layer.Graphics[index]; if (graphic.Data == data) { layer.Graphics.Remove(graphic); DataGraphicObject dataGraphicObject = graphic as DataGraphicObject; if (dataGraphicObject != null) { dataGraphicObject.UnsubscribeOnChange(); } break; } } }
/// <summary> /// React on collection changed. /// </summary> /// <param name="sender">Ignored.</param> /// <param name="e">Event args.</param> private void _CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: { System.Diagnostics.Debug.Assert(e.NewItems.Count > 0); foreach (object data in e.NewItems) { // If graphic for this item was already created (to show not yet committed item) // - do not create it again. if (FindGraphicByData(data) == null) { Graphic graphic = CreateGraphic(data, _dataType, _graphicObjectType, LayerContext); if (graphic != null) { DataGraphicObject dataGraphicObject = graphic as DataGraphicObject; if (dataGraphicObject != null && _layerContext != null) { dataGraphicObject.ObjectContext = _layerContext; } _layer.Graphics.Add(graphic); } } } break; } case NotifyCollectionChangedAction.Remove: { System.Diagnostics.Debug.Assert(e.OldItems.Count > 0); foreach (object data in e.OldItems) { DeleteObject(data, _layer); _graphicDictionary.Remove(data); } break; } case NotifyCollectionChangedAction.Reset: { foreach (Graphic graphic in _layer.Graphics) { DataGraphicObject dataGraphicObject = graphic as DataGraphicObject; if (dataGraphicObject != null) { dataGraphicObject.UnsubscribeOnChange(); } } _graphicDictionary.Clear(); _layer.Graphics.Clear(); break; } default: throw new NotSupportedException(); } }