protected void ItemsModel3D_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Reset: OctreeManager?.Clear(); OctreeManager?.RequestRebuild(); break; } switch (e.Action) { case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Remove: if (e.OldItems != null) { foreach (var item in e.OldItems) { if (mDictionary.ContainsKey(item)) { var model = mDictionary[item]; if (model is GeometryModel3D) { OctreeManager?.RemoveItem(model as GeometryModel3D); } model.DataContext = null; this.Children.Remove(model); mDictionary.Remove(item); } } InvalidateRender(); } break; case NotifyCollectionChangedAction.Reset: var array = this.Children.ToArray(); foreach (var item in array) { item.DataContext = null; this.Children.Remove(item); } mDictionary.Clear(); break; } switch (e.Action) { case NotifyCollectionChangedAction.Reset: if (this.ItemsSource != null) { if (this.ItemTemplate == null) { foreach (var item in this.ItemsSource) { var model = item as Model3D; if (model != null) { this.Children.Add(model); mDictionary.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } else { foreach (var item in this.ItemsSource) { var model = this.ItemTemplate.LoadContent() as Model3D; if (model != null) { model.DataContext = item; this.Children.Add(model); mDictionary.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } InvalidateRender(); break; case NotifyCollectionChangedAction.Add: case NotifyCollectionChangedAction.Replace: if (e.NewItems != null) { if (this.ItemTemplate != null) { foreach (var item in e.NewItems) { if (mDictionary.ContainsKey(item)) { continue; } var model = this.ItemTemplate.LoadContent() as Model3D; if (model != null) { OctreeManager?.AddPendingItem(model); model.DataContext = item; this.Children.Add(model); mDictionary.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } else { foreach (var item in e.NewItems) { if (mDictionary.ContainsKey(item)) { continue; } var model = item as Model3D; if (model != null) { OctreeManager?.AddPendingItem(model); this.Children.Add(model); mDictionary.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } break; } }
private void NodeGroup_OnClear(object sender, OnChildNodeChangedArgs e) { OctreeManager?.Clear(); OctreeManager?.RequestRebuild(); }
/// <summary> /// Handles changes in the ItemsSource property. /// </summary> /// <param name="e"> /// The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data. /// </param> /// <exception cref="System.InvalidOperationException"> /// Cannot create a Model3D from ItemTemplate. /// </exception> private void ItemsSourceChanged(DependencyPropertyChangedEventArgs e) { if (e.OldValue is INotifyCollectionChanged) { (e.OldValue as INotifyCollectionChanged).CollectionChanged -= ItemsModel3D_CollectionChanged; } foreach (Model3D item in Children) { item.DataContext = null; } OctreeManager?.Clear(); mDictionary.Clear(); Children.Clear(); if (e.NewValue is INotifyCollectionChanged) { (e.NewValue as INotifyCollectionChanged).CollectionChanged -= ItemsModel3D_CollectionChanged; (e.NewValue as INotifyCollectionChanged).CollectionChanged += ItemsModel3D_CollectionChanged; } if (ItemsSource == null) { return; } if (this.ItemTemplate == null) { foreach (var item in this.ItemsSource) { if (mDictionary.ContainsKey(item)) { continue; } var model = item as Model3D; if (model != null) { this.Children.Add(model); mDictionary.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } else { foreach (var item in this.ItemsSource) { if (mDictionary.ContainsKey(item)) { continue; } var model = this.ItemTemplate.LoadContent() as Model3D; if (model != null) { model.DataContext = item; this.Children.Add(model); mDictionary.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } if (Children.Count > 0) { OctreeManager?.RequestRebuild(); } }